简体   繁体   中英

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. 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?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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