0

My code looks like this:

public static void DoSomething(TextWriter log)
        {
            log.WriteLine("Hey man! I'm running!");

            try
            {
                using (MyDBContext context = new MyDBContext())
                {
                    MemberMaster mm = context.MemberMaster.SingleOrDefault(m => m.MemberMasterId == 1);

                    if (mm != null)
                    {
                        log.WriteLine(string.Format("MemberMasterId = {0}, Username = {1}", mm.MemberMasterId, mm.UserName));
                    }
                    else
                    {
                        log.WriteLine("Could not find Member with MembermasterId = 1");
                    }
                }
            }
            catch (Exception ex)
            {
                log.WriteLine(ex.Message);
            }

            log.WriteLine("Done!");
        }

MyDBContext.cs looks like this:

public class MyDBContext : DbContext
{
    public MyDBContext() : this("MyDb")
    {

    }

    public MyDBContext(string connectionName) : base(connectionName) { }

    public DbSet<MemberMaster> MemberMaster { get; set; }
} 

I've tried to add a modelBuilder to remove Pluralizing names but that simply results in an error about the DB Context having changed....use code first...etc..etc...

Is there a better way to do this? The WebJob is being deployed via AZure with a .ZIP file and resides in the same AppService as my Website.

1 Answer 1

0

I turn off plurallizing on Sets and Tables by overriding the OnModelCreating, to keep both named the same, and have had no problem:

public class MyDBContext : DbContext
{
    static MyDBContext()
    {
        Database.SetInitializer<DbContext>(null);
    }

    public MyDBContext() : this("MyDb")
    {

    }

    public MyDBContext(string connectionName) : base(connectionName) { }

    public DbSet<MemberMaster> MemberMaster { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        base.OnModelCreating(modelBuilder);
    }
} 
2
  • As indicated in the original post, I've tried to do this but end up with an EF error about Code First Migrations (i.e. the underlying model has changed etc....consider using code first migrations...etc...) There are no changes. I just want to talk to the table. How to get around this 'error' ?
    – RFrey
    Oct 12, 2019 at 20:17
  • Ok, assuming you do not want changes to your model to affect your database, try adding the static constructor above to disable migrations. This is more a database first approach.
    – CJR
    Oct 14, 2019 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.