实体框架5-使用接口类型一对多地编码第一个数组导航属性 [英] Entity Framework 5 - code first array navigation property one to many with Interface Type

本文介绍了实体框架5-使用接口类型一对多地编码第一个数组导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是我的课程:

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; }
}

使用此CommentConfiguration添加到OnModelCreate():

With this CommentConfiguration added to OnModelCreate():

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

我真的不明白为什么注释属性始终为空,以及为什么EF从不初始化它它是虚拟的。
我也尝试禁用延迟加载,但是当我尝试使用 context.Post.Include( Comments)加载导航属性时,错误告诉我不是名为注释的导航属性。
因此,我尝试使用Entity Framework Power Tools Beta 3来查看Entity Data Model,并且我发现表 Post没有导航端,即使两个表之间存在关系并且有Comment

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.

我真心不知道该转向哪种方式,这可能是Array的问题??我应该使用Icollection属性吗?
尽管由于Post正在实现接口,所以我无法更改该属性的类型。

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.

编辑:

此是在查看链接后所做的更改我是昨天发布的。

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; } }
  }

例外是: System.ObjectDisposedException: ObjectContext实例已被处置,不能再用于需要连接的操作,并在应用程序尝试获取注释时引发。
如果删除 virtual 键,该异常消失,但该属性始终为null,并且值不会以任何方式持久。

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
我已经解决了添加新资产并将旧资产映射到它的问题。

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;}
}

在我的PostConfiguration OnModelCreate()中,我使用了将ListComments属性作为这样的导航属性:

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);

现在它可以正常工作,它比我预期的要简单,当我尝试接收评论集时,如果我包含 ListComments属性,则会得到Post数组。
谢谢您的帮助!

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!

推荐答案

我无法访问您评论中的链接,但我想您更改

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.

关于已处置上下文的异常意味着您从数据库提取 Post 个对象并处置该上下文之后,访问此属性。当与数据库的连接丢失时,这将触发延迟加载。解决方案是使用 包含

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.

这篇关于实体框架5-使用接口类型一对多地编码第一个数组导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆