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

寵の児 提交于 2020-05-13 08:03:11

问题


These are my 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 this CommentConfiguration added to OnModelCreate():

HasRequired(c => c.Post)
            .WithMany(b=>(ICollection<CommentInfo>) b.Comments)
            .HasForeignKey(b=>b.PostId)
            .WillCascadeOnDelete(true);

I really cannot understand why the property Comments is always null, and why EF doesn't initialize it since it's virtual. I tried disabling lazy loading too, but when i try loading the navigation property with context.Post.Include("Comments") an error tells me that "There is not a navigation property called Comments". So I tried using Entity Framework Power Tools Beta 3 to see the Entity Data Model, and I discovered that there is not a navigation end for table "Post" even if there is the relationship between the two tables and there's the Comment table end too.

I sincerly don't know which way to turn, could be a problem of Array?? Should I use an Icollection property?? Though I cannot change the type of that property because Post is implementing an Interface.

Every sample I look at is clear and easy to make work. Please help me.. Thank you in advance.

EDIT:

This is what I changed after looking at the link I 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 { 
      get { return Comments ; } 
      set { Comments = (CommentInfo[])value; } }
  }

The exception is: System.ObjectDisposedException :The ObjectContext instance has been disposed and can no longer be used for operations that require a connection and raises when the application tries to get the Comments. If I remove the virtual key the exception disappear but the property remain always null and the values don't persist in any way.

EDITv2 I've solved my problem adding a new property and map my old property to it.

public class Post : IPost
{
    public int Id { get; set; }
    public virtual int[] DuplicateOf { get; set; }
    public ICommentInfo[] Comments
    {
        get { return ListComments.ToArray(); }

    }
    public List<CommentInfo> ListComments {get;set;}
}

In my PostConfiguration OnModelCreate() I used the ListComments property as a navigation property like this:

HasMany(b => b.ListComments)
                .WithRequired(c=>c.Post)
                .HasForeignKey(c=>c.PostId)
                .WillCascadeOnDelete(true);

Now it perfectly works, it was simpler than I expected and when I try to receive the Comments Collection, if I include the "ListComments" property, I get the array of Post. Thank you for your help!


回答1:


I can't access the link in your comment, but I assume you changed

public virtual ICommentInfo[] Comments { get; set; }

into the common way to define navigation properties:

public virtual ICollection<CommentInfo> Comments { get; set; }

because entity framework does not support interfaces in its conceptual model.

The exception about the disposed context means that you access this property after fetching Post objects from the database and disposing the context. This triggers lazy loading while the connection to the database is lost. The solution is to use Include:

var posts = context.Posts.Include(p => p.Comments).Where(...)

Now posts and comments are fetched in one go.



来源:https://stackoverflow.com/questions/14790960/entity-framework-5-code-first-array-navigation-property-one-to-many-with-inter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!