EF 6 Code First Mapping one-to-many

107 views Asked by At

Can someone please help me with mapping the following hierarchy with EF 6 code first? I can find anything useful for the below example in the documentation.

namespace Contacts {
    public class Person
    {
        public Person()
        {
            this.Emails = new HashSet<Email>();
        }

        public long Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual ICollection<Email> Emails { get; set; }
    }

    public class Company
    {
        public Company()
        {
            this.Emails = new HashSet<Email>();
        }

        public long Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Email> Emails { get; set; }
    }


    public class Email
    {
        public string Value { get; set; }
        public string Label { get; set; }
        public string TargetId { get; set; }
        public string TargetType { get; set; }
    }
}

TargetType can be set to Company or Person depending on the entity that contains the Email instance.

DB Schema:

enter image description here

0

There are 0 answers