简体   繁体   English

实体框架在匹配实体名称时将属性名称附加“1”

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

I am using Entity Framework version 4.0 to create a model using the database first approach. 我正在使用Entity Framework 4.0版使用数据库第一种方法创建模型。 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 State with State State和StateName
  • table Status with columns Status and Description table列状态和描述的状态

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. 问题是,当其中一个表被导入EF模型时,这些列被映射的属性名称会在它们的末尾附加一个“1”。

So I end up with 所以我最终得到了

  • entity State with properties State1 and StateName 具有属性State1和StateName的实体State
  • entity Status with properties Status1 and Description 实体状态,包含属性Status1和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." 当我尝试删除末尾的“1”时,我收到一条消息“在这种情况下,名称状态不能重复。请选择其他名称。”

Is there a way for me to have my properties keep their names or is this a documented limitation in the framework? 有没有办法让我的财产保留其名称,或者这是框架中记录的限制?

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; 
}

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. 我错过了主键被映射到基类中的“int id”字段的事实。 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. 我错误地认为我需要将“int MyEntityKey”属性添加到MyEntity。 It conflicted with the base id map and incremented the property I added. 它与基本id映射冲突并增加了我添加的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM