Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在我的孩子中将依赖项设置为ForeignKey_C#_Entity Framework_Ef Code First - Fatal编程技术网

C# 如何在我的孩子中将依赖项设置为ForeignKey

C# 如何在我的孩子中将依赖项设置为ForeignKey,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我需要id从我的父母那里获取数据集合。 计划是使用ParentId从存储库中获取所有好友 为了说明这一点,我有一个父类和子类,还有一个映射器。 我想我必须在ParentMapper中定义键 请参见我的代码中的注释 { public class Child { public int IdChild { get; set; } public string ChildName {get;set;} // h

我需要id从我的父母那里获取数据集合。 计划是使用ParentId从存储库中获取所有好友

为了说明这一点,我有一个父类和子类,还有一个映射器。 我想我必须在ParentMapper中定义键

请参见我的代码中的注释

    {
        public class Child {
            public int IdChild { get; set; }
            public string ChildName {get;set;}
            // here is the problem
            //I need to define ParentID as ForeignKey in some way but how??
            //I belive its done in the ParentMapper
            public virtual int ParentId { get; set; } //ForeignKey  

            public virtual ICollection<Friend>   Friends        { get; set; }

            //The plan is to fetch all friends from the repository by using the ParentId and 
            //keep the entity as clean as possible.
            public virtual ICollection<Friend>   FamilyFriends  { get; set; }
        }
        public class Parent {
            public int IdParent { get; set; }
            public string ParentName {get;set;}

            public virtual ICollection<Friend>   Friends        { get; set; }
            public virtual ICollection<Child>    Children        { get; set; }
        }
    }


    {
        class ParentMapper : EntityTypeConfiguration<Parent>
        {
            public ParentMapper()
            {
                HasKey(one => one.IdParent);

                //I started out like this but its not possible....
                //But this will give an error obviusly
                HasMany(c => c.Children).WithRequired(d => d.ParentId).HasForeignKey(one => one.ParentId);
            }
        }
    }

    {
        class ChildMapper : EntityTypeConfiguration<Child>
        {
            public ChildMapper()
            {
                HasKey(one => one.IdChild);
            }
        }
    }
{
公营儿童{
public int IdChild{get;set;}
公共字符串ChildName{get;set;}
//问题就在这里
//我需要以某种方式将ParentID定义为ForeignKey,但如何定义??
//我相信它是在ParentMapper中完成的
公共虚拟int-ParentId{get;set;}//ForeignKey
公共虚拟ICollection好友{get;set;}
//计划是使用ParentId和
//保持实体尽可能干净。
公共虚拟ICollection FamilyFriends{get;set;}
}
公共类父类{
公共int IdParent{get;set;}
公共字符串ParentName{get;set;}
公共虚拟ICollection好友{get;set;}
公共虚拟ICollection子项{get;set;}
}
}
{
类ParentMapper:EntityTypeConfiguration
{
公共ParentMapper()
{
HasKey(one=>one.IdParent);
//我是这样开始的,但这是不可能的。。。。
//但这显然会产生一个错误
HasMany(c=>c.Children)。WithRequired(d=>d.ParentId)。HasForeignKey(one=>one.ParentId);
}
}
}
{
类ChildMapper:EntityTypeConfiguration
{
公共子映射器()
{
HasKey(one=>one.IdChild);
}
}
}
公共类子类{
public int ChildId{get;set;}
公共字符串ChildName{get;set;}
[外键(“母公司”)]
public int ParentId{get;set;}//ForeignKey
公共父级{get;set;}
公共列表朋友{get;set;}
公共列表FamilyFriends{get;set;}
}
公共类父类{
public int ParentId{get;set;}
公共字符串ParentName{get;set;}
公共列表朋友{get;set;}
公共列表子项{get;set;}
}
公课朋友
{
public int-FriendId{get;set;}
公共字符串FriendName{get;set;}
[外键(“母公司”)]
public int ParentId{get;set;}
公共好友父项{get;set;}
[外国钥匙(“儿童”)]
public int ChildId{get;set;}
[反向财产(“朋友”)]
公共子项{get;set;}
[外国钥匙(“儿童基金会”)]
public int ChildFamId{get;set;}
[反向财产(“家庭朋友”)]
公共子ChildFam{get;set;}
}

是否是
父对象。朋友
也打算成为映射集合?(也就是说,朋友对父母或孩子也有FK吗?)嗨,格特·阿诺德,好问题:)好吧,去埃森家我把它漏掉了,因为我不认为这是问题的一部分,或者可能是。。。无论如何,是的,它也打算成为一个映射集合。那将是相当复杂的。我不确定你是否意识到其中的含义
Friend
将有三个FK(父FK,两个FK用于
Child
中的两个集合),这三个FK应为空且相互排斥。(即,每个记录中只能有一个值)。如果父母只通过孩子找到朋友会更简单。如果你愿意,我可以用数据注释来写这篇文章。)谢谢@Elvin Arzumanoğlu,那太好了。有些DB支持多重关系,但有些不支持。这很好@Elvin Arzumanoğlu,我会尝试一下,但使用映射文件。我看到的逆属性是我必须利用的东西。几天后我会回来更新我的结果。谢谢几天后dddd有趣的是,迟做总比不做好@Elvin Mammadov,生活中发生的事情是无法预测的,但现在我回来了。
public class Child {

        public int ChildId { get; set; }

        public string ChildName {get;set;}

        [ForeignKey("Parent")]
        public int ParentId { get; set; } //ForeignKey  
        public Parent Parent{get; set;}


        public List<Friend>   Friends        { get; set; }

        public List<Friend>   FamilyFriends  { get; set; }
    }
    public class Parent {
        public int ParentId { get; set; }
        public string ParentName {get;set;}

        public List<Friend>   Friends        { get; set; }
        public List<Child>    Children        { get; set; }
    }

    public class Friend
    {
        public int FriendId { get; set; }
        public string FriendName {get;set;}

        [ForeignKey("Parent")]
        public int ParentId{get;set;}
        public Friend Parent {get;set;}

        [ForeignKey("Child")]
        public int ChildId{get;set;}

        [InverseProperty("Friends")]
        public Child Child {get;set;}

         [ForeignKey("ChildFam")]
        public int ChildFamId{get;set;}

        [InverseProperty("FamilyFriends")]
        public Child ChildFam {get;set;}
    }