问题
I am using Entity Framework version 4.0 to create a model using the database first approach. In the database, there's a number of tables that contain columns named the same as their parent table.
So for example we have
- table State with columns State and StateName
- table Status with columns Status and Description
The issue is that when one of these tables is imported into the EF model, the property names that those columns get mapped to get a '1' appended to the end of them.
So I end up with
- entity State with properties State1 and StateName
- entity Status with properties Status1 and Description
When I attempt to remove the '1' at the end, I get a message saying "The name Status cannot be duplicated in this context. Please choose another name."
Is there a way for me to have my properties keep their names or is this a documented limitation in the framework?
回答1:
You can't have a member in your class which is named like your class is.
Example:
class Test
{
// Invalid
int Test;
// Invalid
public int Test {get; set; }
// OK
int Test1;
}
回答2:
For future reference... My issue was solved by deletion. I missed the fact that the primary key was mapped to an "int id" field in the base class. When I saw the pattern
this.Property(t => t.id).HasColumnName("MyEntityKey");
I erroneously assumed that I needed to add the "int MyEntityKey" property to MyEntity. It conflicted with the base id map and incremented the property I added.
来源:https://stackoverflow.com/questions/33348019/entity-framework-appending-1-to-property-name-when-it-matches-entity-name