Entity Framework 4.1 Code first mapping to tables that have their primary key as the foreign key column

StackOverflow https://stackoverflow.com/questions/9300171

Question

I have an existing database that I'm using Entity Framework Code First to map. The naming convention for columns is odd, so I decided I'd map entity properties manually, and up until now this has been fine.

The schema for the database is fairly strange to me and is definitely not how I would've done it. Unfortunately, I'm stuck with it for the time being.

Basically there is a single primary key (AccountNumber) shared by a number of tables creating a bunch of one-to-one relationships. However, the primary key is also the foreign key column. Here is what my entities look like (with a whole bunch of properties removed for simplicity). I've only included two entities to make it easy.:

public class Customer
{
    public int AccountNumber { get; set; }
    public String PhoneNumber { get; set; }
    ...
    public virtual Address Address { get; set; }
}

public class Address
{
    public int AccountNumber { get; set; }
    public String Name { get; set; }
    public String Address1 { get; set; }
    public String City { get; set; }
    ...
    public virtual Customer Customer { get; set; }
}

The two entities share the same primary key. I've created configuration classes to do the mapping like this:

public class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
    public CustomerConfiguration()
        : base()
    {
        HasKey(p => p.AccountNumber);
        Property(p => p.AccountNumber).
            HasColumnName("cm_l_acct").
            IsRequired();
        Property(p => p.PhoneNumber).
            HasColumnName("cm_s_phonenumber");

        HasRequired(x => x.Address).
            WithRequiredPrincipal(x => x.Customer).
            Map(x => x.MapKey("am_l_acct"));
    }
}
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
    public AddressConfiguration()
        : base()
    {
        HasKey(p => p.AccountNumber);
        Property(p => p.AccountNumber).
            HasColumnName("am_l_acct").
            IsRequired();

        ...
    }
}

The foreign key mapping is only done on one side. This appears like it would work if not for the fact that the foreign key column is also the primary key of the table. When I try to run a query, I get the error:

(256,6): error 0019: Each property name in a type must be unique. Property name 'am_l_acct' was already defined.

Unfortunately, I can't pull the mapping for the AccountNumber property off of the Address entity because it is the primary key.

Is there a way I can accomplish this mapping, or is it impossible?

Was it helpful?

Solution

Removet this Map(x => x.MapKey("am_l_acct")) from your Customer mapping. This mapping is only used if you map want to define FK column in database and you don't have FK property in the class but you have it - it is primary key in the Address entity. If you try to map FK that way EF thinks that you are trying to create to columns with the same name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top