user1416776
user1416776

Reputation: 99

How do i set dependency to ForeignKey in my child

I need the id to fetch data collections from my parent. The plan is to fetch all friends from the repository by using the ParentId.

To illustrate I have a Parent and Child class and also one Mapper each. I suppose I must define the key in the ParentMapper.

See comment in my code

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

Upvotes: 2

Views: 137

Answers (1)

Elvin Mammadov
Elvin Mammadov

Reputation: 27387

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

Upvotes: 1

Related Questions