Entity Framework throws exception - Invalid object name 'dbo.BaseCs'

后端 未结 12 855
挽巷
挽巷 2020-11-30 11:59

I\'ve followed Adam\'s answer here and the Entity Framework now works and the Seed() method also works.

But when I try to access the database l

相关标签:
12条回答
  • 2020-11-30 12:44

    In the context definition, define only two DbSet contexts per context class.

    0 讨论(0)
  • 2020-11-30 12:45

    If everything is fine with your ConnectionString check your DbSet collection name in you db context file. If that and database table names aren't matching you will also get this error.

    So, for example, Categories, Products

    public class ProductContext : DbContext 
    { 
        public DbSet<Category> Categories { get; set; } 
        public DbSet<Product> Products { get; set; } 
    }
    

    should match with actual database table names:

    0 讨论(0)
  • 2020-11-30 12:52

    I don't know if is the case,

    If you create a migration before adding a DbSet your sql table will have a name of your model, generally in singular form or by convention we name DbSet using plural form.

    So try to verifiy if your DbSet name have a same name as your Table. If not try to alter configuration.

    0 讨论(0)
  • 2020-11-30 12:58

    For what it is worth, I wanted to mention that in my case, the problem was coming from an AFTER INSERT Trigger!

    These are not super visible so you might be searching for a while!

    0 讨论(0)
  • 2020-11-30 12:58

    My fix was as simple as making sure the correct connection string was in ALL appsettings.json files, not just the default one.

    0 讨论(0)
  • 2020-11-30 12:59

    It might me an issue about pluralizing of table names. You can turn off this convention using the snippet below.

     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
         base.OnModelCreating(modelBuilder);
         modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
     }
    
    0 讨论(0)
提交回复
热议问题