Sunday, 15 January 2012

Entity Framework 5 - code first array navigation property one to many with Interface Type -



Entity Framework 5 - code first array navigation property one to many with Interface Type -

these classes:

public class post : ipost { public int id { get; set; } public virtual int[] duplicateof { get; set; } public virtual icommentinfo[] comments { get; set; } } public class commentinfo : icommentinfo { public virtual string author { get; set; } public virtual int id { get; set; } public virtual string text { get; set; } public virtual int postid{ get; set; } [foreignkey("postid")] public virtual post post { get; set; } }

with commentconfiguration added onmodelcreate():

hasrequired(c => c.post) .withmany(b=>(icollection<commentinfo>) b.comments) .hasforeignkey(b=>b.postid) .willcascadeondelete(true);

i cannot understand why property comments null, , why ef doesn't initialize since it's virtual. tried disabling lazy loading too, when seek loading navigation property context.post.include("comments") error tells me "there not navigation property called comments". tried using entity framework powerfulness tools beta 3 see entity info model, , discovered there not navigation end table "post" if there relationship between 2 tables , there's comment table end too.

i sincerly don't know way turn, problem of array?? should utilize icollection property?? though cannot alter type of property because post implementing interface.

every sample @ clear , easy create work. please help me.. give thanks in advance.

edit:

this changed after looking @ link posted yesterday.

public class post : ipost { public int id { get; set; } public virtual int[] duplicateof { get; set; } public virtual icollection<commentinfo> comments { get; set; } icommentinfo[] ipost.comments { { homecoming comments ; } set { comments = (commentinfo[])value; } } }

the exception is: system.objectdisposedexception :the objectcontext instance has been disposed , can no longer used operations require connection , raises when application tries comments. if remove virtual key exception disappear property remain null , values don't persist in way.

editv2 i've solved problem adding new property , map old property it.

public class post : ipost { public int id { get; set; } public virtual int[] duplicateof { get; set; } public icommentinfo[] comments { { homecoming listcomments.toarray(); } } public list<commentinfo> listcomments {get;set;} }

in postconfiguration onmodelcreate() used listcomments property navigation property this:

hasmany(b => b.listcomments) .withrequired(c=>c.post) .hasforeignkey(c=>c.postid) .willcascadeondelete(true);

now works, simpler expected , when seek receive comments collection, if include "listcomments" property, array of post. give thanks help!

i can't access link in comment, assume changed

public virtual icommentinfo[] comments { get; set; }

into mutual way define navigation properties:

public virtual icollection<commentinfo> comments { get; set; }

because entity framework not back upwards interfaces in conceptual model.

the exception disposed context means access property after fetching post objects database and disposing context. triggers lazy loading while connection database lost. solution utilize include:

var posts = context.posts.include(p => p.comments).where(...)

now posts , comments fetched in 1 go.

ef-code-first code-first entity-framework-5 foreign-key-relationship navigation-properties

No comments:

Post a Comment