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