Entity Framework 5 - navigation property of first one to many code array with interface type

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 can't figure out why the Comments property is always null, and why EF doesn't initialize it as it is virtual. I also tried to disable lazy loading, but when I try to load the navigation property with context.Post.Include("Comments")

, the error tells me "there is no navigation property called comments". So I tried to use the Entity Framework Power Tools Beta 3 to see the entity data model and I found that there is no navigation end for the Publish table even if there is a relationship between the two tables and there is also the end of the comment table.

I honestly don't know which way to go, maybe there is a problem with the array? Should I be using the Icollection property? Although I cannot change the type of this property, because Post implements the interface.

Every sample I look at is clear and easy to work with. Please help me .. Thanks in advance.

EDIT:

This is what I changed by looking at the 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 { 
      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 is thrown when the application tries to fetch comments. If I remove the key virtual

, the exception is gone, but the property is always null and no values ​​persist.

EDITv2 I solved the problem by adding a new property and mapping my old property to me.

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 works great, it was simpler than I expected, and when I try to get the collection of comments, if I include the ListComments property, I get the Post array. Thanks for the help!

+3


source to share


1 answer


I cannot access the link in your comment, but suppose you changed

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

      

into a general way of defining navigation properties:

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

      



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

An exception regarding the selected context means that you access this property after you retrieve Post

objects from the database and remove the context. This causes lazy loading when the database connection is lost. The solution is to use Include

:

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

      

Now messages and comments are displayed in one go.

+3


source







All Articles