Entity Framework 4 - Code First not storing inherited table separate from base table -
EF code I have created a simple project with CTP 4. I have two classes in this project, which are inherited from each other Found in
I want to store data in different tables, but the default of EF4 is to map / map these two organizations in the same table.
.toTable () , I can change this behavior, but with this I have a side effect: when I receive a traditionally received object IF I am going to set some information in mapping, but I do not know.
Fixed zero main string [] args) {Database.SetInitializer & LT; ZooContext & gt; (New RecreateDatabaseIfModelChanges & lt; ZooContext & gt; ()); (ZooContext ctx = New ZooContext ()) using {mammals = new mammals (); Mam.NumberOfLegs = 4; Ctx.Animals.Add (m); // ctx.mammals.Add (mam) similar behavior ctx.SaveChanges (); }} Public Square ZooContext: DbContext {Public DbSet & lt; Animals & gt; Animals {receive; Set; } Public DbSet & lt; Blue & gt; Receive mammals; Set; } Protected Override Zero OnModelCreating (System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) {modelBuilder.Entity & LT; Animals>. () HasKey (P = & gt; p.Id). MapHierarchy () Case & lt; Animals & gt; (A = & new; new {a.Id}); Modlbilder Anti Lieutenant; Animals & gt; (). Hesky (P => PIID). MapHierarchy () Case & lt; Blue & gt; (A = & new; new {a.Id, a.NumberOfLegs}). ToTable ("mammals"); Base.OnModelCreating (modelBuilder); }} Public class animal {public virtual id id {get; Set; }} Public Class Mammal: Animals {Public Entry Number of Legs {get; Set; }}
After
is actually aware of CTP4, but did it in CTP5 Here's an example of what you want to do:
public >>
Class mammals {public integer id {receipt; Set; } Public string name {get; Set; }} Public Squared Animal: Mammals {Public Entry Number of Legs {get; Set; }} Public Square ZooContext: DbContext {Public DbSet & lt; Mammal & gt; Receive mammals; Set; } Public DbSet & lt; Animals & gt; Animals {receive; Set; } Protected Override Zero OnModelCreating (ModelBuilder Builder) {builder.Entity & lt; Mammal & gt; () .map & lt; Animals & gt; (X = & gt; x.ToTable ("Animals")) .map & lt; Mammal & gt; (X = & gt; x.ToTable ("mammals")); Base.OnModelCreating (Builder); }} Class Program {Static Wide Men} {DbDatabase.SetInitializer (New Drop-CreteDatabase Mbodel Change & lt; Zoo Contex & gt; ()); {Ents.Mammals.Add (new animal {name = "first mutant animal", number of legs = 10}) using Var ents = new ZooContext ()); Friends Mammals. Add (new mammal {name = "first mammal"}); Ents Mammals. Add (new mammals {name = "second mammal"}); Ents.SaveChanges (); }}}
PS: I used SqlCe 4.0 as the database engine. It fails to start, if the mammal
class has only id
and no other properties (why not know). So I added the name
property base class. I'm not sure that the weather is problem in CTP4, SqlCe 4.0, or I do not know anything.
Hope this will be helpful.
Comments
Post a Comment