EF 6 Code First Mapping一对多

时间:2015-06-16 12:34:02

标签: entity-framework one-to-many code-first

有人可以先帮助我使用EF 6代码映射以下层次结构吗?我可以在文档中找到对以下示例有用的任何内容。

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可以设置为Company或Person,具体取决于包含Email实例的实体。

DB Schema:

enter image description here

0 个答案:

没有答案
相关问题