c# - How can I have the primary key of one entity be another entity in EF4 code-first? -


say have post entity, , want store changes post objects, have posthistory entity. right define history this:

public class posthistory {    public virtual post post { get; set; }     //... properties of posthistory object } 

the problem can't figure out how work ef4 code-first. @ first model building fails because there no primary key defined posthistory. in context call

modelbuilder.entity<posthistory>().haskey(x => x.post); 

and following exception occurs:

system.missingmethodexception: no parameterless constructor defined object.. 

that exception not talking posthistory object not having parameterless constructor, because added 1 , didn't help.

any ideas how accomplish this?

public class post {     [key, databasegenerated(databasegenerationoption.identity)]     public int id { get; set; }      public virtual icollection<posthistory> histories { get; set; } }  public class posthistory {     [key, databasegenerated(databasegenerationoption.identity)]     public int id { get; set; }      public int postid { get; set; }     public virtual post post { get; set; }     //... properties of posthistory object } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -