EF 4.1 Code First. One type inheritance with a different primary key name from the base class base name

Considering this:

create table Location( LocationId int identity(1,1) not null primary key, Address nvarchar(max) not null, City nvarchar(max) null, State nvarchar(max) not null, ZipCode nvarchar(max) not null ); create table Park( ParkId int not null primary key references Location(LocationId), Name nvarchar(max) not null ); 

I tried this mapping:

 modelBuilder.Entity<Location>(); modelBuilder.Entity<Park>().ToTable("Park"); modelBuilder.Entity<Park>().Property(x => x.LocationId).HasColumnName("ParkId"); 

Unfortunately, this did not work.

 using (var db = new Ef()) { var park = new Park { Name = "11th Street Park", Address = "801 11th Street", City = "Aledo", State = "TX", ZipCode = "76106" }; db.Set<Location>().Add(park); db.SaveChanges(); } 

He has this error:

The LocationId property is not a declared property in the Park type. Verify that the property has not been explicitly excluded from the model using the Ignore or NotMappedAttribute annotation data. Verify that this is a valid primitive property.

How do I map a Park object so that its LocationId property falls into the ParkId column?

I have this mapping, by the way:

 public class Location { public virtual int LocationId { get; set; } public virtual string Address { get; set; } public virtual string City { get; set; } public virtual string State { get; set; } public virtual string ZipCode { get; set; } } public class Park : Location { public virtual string Name { get; set; } } 

If this can help, it is possible in EF 4.0 (via the constructor), just follow the steps described in Chapter 2-11 of Entity Framework 4.0 Recipes, an approach to solving problems. Now I am trying to use the code first through EF 4.1

[EDIT]

If I change ParkId to LocationId, everything will be fine. However, using the design approach, you can map the LocationId to the ParkId from the Park table; I want to achieve the same code first

 create table Park( LocationId int not null primary key references Location(LocationId), Name nvarchar(max) not null ); 
+4
source share
2 answers

As I know (and I tried this several times), the code at first does not support this => your derived type should use the same column names for the primary key.

This problem can be described very simply: the current current implementation of the mapping does not allow overriding the matching rules from the parent => the parent determines the column names of the primary key in all derived entities.

IMO, the most likely reason is that it was really designed as code, at the beginning where you do not have an existing database, and you do not need to worry about naming the database - you had to define EF to determine the names. After the DbContext API was released, people started using it with an existing database. But here a problem arises: the initial use cases are not taken into account, so some scripts that are pretty easy to execute in EDMX are not possible. This is one of them.

+3
source

The following is a workaround for this problem:

Create a view for the view and map the entity class that is viewing. Rename the key column in your view so that it matches the key column in the base table.

eg:

User base table (UserID, FirstName, LastName)

View Manager (ManagerID, DepartmentID)

Entity Framework cannot update dispatcher because key column is different!

decision:

 create view UserManager as select ManagerID as UserID, DepartmentID from Manager 

Then map the Manager class to the UserManager view, and not to the Manager table.

0
source

All Articles