序列不包含元素:首先自引用代码 [英] Sequence contains no elements : self referencing code first

查看:154
本文介绍了序列不包含元素:首先自引用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用下一个实体进行自我引用

when I try to do self referencing with next entities

public class Folder
{
    public int Id { get; set; }
    public int?  ParentFolderId { get; set; } // Added property
    public string Name { get; set; }


    public virtual ICollection<Folder> Childrens { get; set; } // Added property
    public virtual ICollection<ArticleTitle> ArticleTitles { get; set; }

}


public class ArticleTitle
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int? FolderId { get; set; }
    public virtual Folder Folder { get; set; }
    public virtual Article Article { get; set; }
}

我添加了几个属性(注释添加属性)和这行代码:

I added few properties (commented 'added property') and this line of code:

  modelBuilder.Entity<Folder>()
 .HasMany(s => s.Childrens)
 .WithOptional()
 .HasForeignKey(s => s.ParentFolderId);

得到这个例外:

< b>序列不包含元素

是否自动引用不正确?

推荐答案

这是你的代码如何:

public class Folder
{
    public int Id { get; set; }
    [ForeignKey("ParentFolder")]
    public int?  ParentFolderId { get; set; }
    public virtual Folder ParentFolder { get; set; }
    public string Name { get; set; }


    public virtual ICollection<Folder> Childrens { get; set; }
    public virtual ICollection<ArticleTitle> ArticleTitles { get; set; }

}
...

我很高兴它有帮助。

这篇关于序列不包含元素:首先自引用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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