Domanda

I had a Dbset declared like so:

public DbSet<James_BobRelation> James_Bobs { get; set; }

my Sql table is called James_Bobs

the class holding the data was called James_BobRelation

This does not work... when making calls to this within a repository:

EFDbContext context = new EFDbContext();
context.James_Bobs;
// It thinks the table is called James_BobRelation instead?

and yet when I changes the class name to James_Bobs, it did work?

What's the rules?


Further more, this totally confused me... I have a database table called Sausages

the DbSet is like so:

public DbSet<Sausage> Sausages { get; set; }

When I called upon Sausages that actually worked! even though the class in this case was Sausage... does Entity framework add the 's' if it thinks it can? it's SOO CONFUSING ME

È stato utile?

Soluzione

In the first case, it will be looking for James_BobRelations by default; you can change that behavior:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<James_BobRelation>().MapSingleType().ToTable("TableNameHere");
}

Basically the default behavior is to pluralize your object and infer the table name from that. IE it will infer a name of Sausages for a class named Sausage.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top