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
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.BaseCs'
This error means that EF is translating your LINQ into a sql statement that uses an object (most likely a table) named dbo.BaseCs
, which does not exist in the database.
Check your database and verify whether that table exists, or that you should be using a different table name. Also, if you could post a link to the tutorial you are following, it would help to follow along with what you are doing.
If you are providing mappings like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ClassificationMap());
modelBuilder.Configurations.Add(new CompanyMap());
modelBuilder.Configurations.Add(new GroupMap());
....
}
Remember to add the map for BaseCs.
You won't get a compile error if it is missing. But you will get a runtime error when you use the entity.
EF is looking for a table named dbo.BaseCs. Might be an entity name pluralizing issue. Check out this link.
EDIT: Updated link.
It is most likely a mismatch between the model class name and the table name as mentioned by 'adrift'. Make these the same or use the example below for when you want to keep the model class name different from the table name (that I did for OAuthMembership). Note that the model class name is OAuthMembership whereas the table name is webpages_OAuthMembership.
Either provide a table attribute to the Model:
[Table("webpages_OAuthMembership")]
public class OAuthMembership
OR provide the mapping by overriding DBContext OnModelCreating:
class webpages_OAuthMembershipEntities : DbContext
{
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
var config = modelBuilder.Entity<OAuthMembership>();
config.ToTable( "webpages_OAuthMembership" );
}
public DbSet<OAuthMembership> OAuthMemberships { get; set; }
}
You have to define both the schema and the table in two different places.
the context defines the schema
public class BContext : DbContext
{
public BContext(DbContextOptions<BContext> options) : base(options)
{
}
public DbSet<PriorityOverride> PriorityOverrides { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.HasDefaultSchema("My.Schema");
builder.ApplyConfiguration(new OverrideConfiguration());
}
}
and for each table
class PriorityOverrideConfiguration : IEntityTypeConfiguration<PriorityOverride>
{
public void Configure(EntityTypeBuilder<PriorityOverride> builder)
{
builder.ToTable("PriorityOverrides");
...
}
}
Instead of
modelBuilder.Entity<BaseCs>().ToTable("dbo.BaseCs");
Try:
modelBuilder.Entity<BaseCs>().ToTable("BaseCs");
even if your table name is dbo.BaseCs