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
In the context definition, define only two DbSet contexts per context class.
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:
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.
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!
My fix was as simple as making sure the correct connection string was in ALL appsettings.json files, not just the default one.
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>();
}