When querying SQL with EF6 from a WebJob, query fails because it's trying to get a pluralized version of the table

entity-framework-6 webjob

Question

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
0
10/5/2019 3:47:28 PM

Popular Answer

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);
    }
} 
0
10/14/2019 12:21:09 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow