entity framework - EF 4.1 Code First. Table-per-type inheritance with different primary key name from its base class' primary key name -
Given this:
create table location (location id int identity (1,1 ) No zero primary key, address nvarchar (maximum) zero, city nvarchar (maximum) null, state nvarchar (maximum) zero, zipcode nvarchar (maximum) zero); Table-Making Park (ParkID Int. Null Primary Key Reference Location (Location ID), Name Navratra (max.) Faucet); I tried this mapping:
modelbuilder.Entity & lt; Location & gt; (); ModelBuilder.Entity & LT; Park & gt; () ToTable ("Park"). Modelbuilder.Entity & lt; Park & gt; (). Property (x = & gt; x.LocationId) .HasColumnName ("ParkId"); Unfortunately this is not working. Using
(var db = new if ()) {var park = new park {name = "11th Street Park", address = "801 11th Street", city = "Aleado ", State =" TX ", zipcode =" 76106 "}; Db.Set & LT; Location & gt; () Add (park). Db.SaveChanges (); } There is this error in:
Property 'Location ID' type is not a declared property on 'park' Verify that the property is ignored or NotMappedAttribute has not been explicitly excluded from the model using data annotation. Make sure this is a valid primitive asset.
Should I map the park unit so that its location ID property comes to the Park ID column?
Let me direct this mapping:
public class space {public virtual int location command {receipt; 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 Square Park: Location {public virtual string name {get; Set; }} If it can help, then it is possible in EF 4.0 (by the designer), just follow the steps given in Chapter 2-11 of Unit Framework 4.0 dishes, Problem Solving Approach Now I'm trying it on the first code by EF 4.1
[edit] If I change ParkId to the place id So, things are fine. However, with the designer approach, it is possible to map the location ID to ParkId of Table Park; I want to get the same thing with the code for the first time
to create a table (place id IIT Intel tap primary key reference location (location ID), do not tap nvarchar (max)) ; As I know (and I've tried it many times) does not support the first code I
this is> & gt; Your derived type should use the same column names for the primary key.
This problem can be described very easily: Current Fluent Mapping Implement Parents Unit = & gt; Does not allow override to mapping rules. The parent unit determines the name of the primary key in all the derivative entities.
IMO The most likely reason is that it was actually created as the first code, where you do not have an existing database and you do not have to bother the database naming - it's defined as the name as needed After the release of the DbContext API on EF, people started to use extensively with the existing database. But here comes a problem: In cases of early use it has not been counted, so some scenarios which are very easily done in EDMX are not possible. This is one of those.
Comments
Post a Comment