Entity Framework appending '1' to property name when it matches entity name

北慕城南 提交于 2019-12-12 11:22:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!