Entity framework, discriminator column, but no inheritance

前端 未结 1 1337
Happy的楠姐
Happy的楠姐 2021-02-20 11:56

I want to test a very simple Code-First example. I have a class called PurchaseItem which is not inherited from any based class and also no other class inherits fro

相关标签:
1条回答
  • 2021-02-20 12:22

    Your connection string might differ from your context! Please check this article.

    Entity Framework follows the Convention over Configuration school of thought. This means that even in finding the related connection string, Entity Framework, uses the name of your context class (in this case MiniAccounting) and tries to find a connection string with that name in the configuration file.

    However, if it doesn't find anything, it's fallback to light version of SQL (I guess SQL CE, or SQL Express, not sure about it), creates a database by itself, and tries to insert into it.

    In your case, check the configuration file, and see if it matches the name of your context class. If not, user this constructor:

    public class MiniContext : DbContext
    {
        public MiniContext()
             : base("YourConnectionStringNameHere")
        {
            Database.SetInitializer<MiniContext>(null);
        }
    
        public DbSet<PurchaseItem> PurchaseItems { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题