Asp核心多实体关系

时间:2017-07-17 02:47:57

标签: asp.net-core entity-framework-core

我正在研究联系信息结构的建模,并且还没有弄清楚如何使用EF Core编码关系。我很擅长将EF用于数据访问层。

我想要一个可以包含网站,电话号码,电子邮件或社交信息的联系模式。然后,联系信息将添加到几个不同的模型中。任何建议都会有所帮助,我不确定如何将这个一对多代码与许多表关系进行编码,或者甚至可以使用EF进行编码。

到目前为止的模型

 public class Contact
{
    public String Id { get; set; }
    public Int32 ContactType { get; set; } //Enum for Website, Phonenumbers, Emails, or Social
    public String RecId { get; set; } //FK to multiple Models
    public String RecType { get; set; }//Value for which model the RecID is for
    public String Name { get; set; }
    public String Value { get; set; }
}

 public class ContactInfo
{
    public virtual IList<Contact> Website { get; set; } 
    public virtual IList<Contact> PhoneNumbers { get; set; }
    public virtual IList<Contact> Emails { get; set; }
    public virtual IList<Contact> Socials { get; set; }
}
//Example of models to use the contact model
public class Company
{
  ....
  pubic ContactInfo ContactInfo { get; set;}
 }
public class Client
{
  ....
  pubic ContactInfo ContactInfo { get; set;}
 }

2 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,那么您可以使用以下代码示例,但这并不是您要实现的目标。这可能会让您了解EF需要做什么。

public class Contact
    {
        public String Id { get; set; }
        public ContactType ContactType { get; set; } //Enum for Website, Phonenumbers, Emails, or Social
        public String RecId { get; set; } //FK to multiple Models (This can't be the FK to multiple table as it should be FK for one table so that FK for Company would be CompanyId, FK for the Client should ClientId)
        public String RecType { get; set; }//Value for which model the RecID is for (This need to rethink as it may not needed.)
        public String Name { get; set; }
        public String Value { get; set; }

        // One to Many Relationship

        public string CompanyId? { get; set; }
        public string ClientId? { get; set; }

        public Company Company { get; set; }
        public Client Client { get; set; }
    }

    public class Company
    {
        public String Id { get; set; }
        // Other properties

        // One to Many Relationship
        public ICollection<Contact> Contacts { get; set; }
    }

    public class Client
    {
        public String Id { get; set; }
        // Other properties

        // One to Many Relationship
        public ICollection<Contact> Contacts { get; set; }
    }


    /* Db context */

    public class YourDbContext : DbContext
    {
        public YourDbContext(DbContextOptions<YourDbContext> options)
            : base(options)
        {

        }

        public virtual DbSet<Contact> Contacts { get; set; }

        public virtual DbSet<Company> Companies { get; set; }

        public virtual DbSet<Client> Clients { get; set; }



        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Contact>().HasKey(t => t.Id);

            modelBuilder.Entity<Company>().HasKey(t => t.Id);
            modelBuilder.Entity<Company>().HasMany(c => c.Contacts).WithOne(c => c.Company).HasForeignKey(k => k.CompanyId);

            modelBuilder.Entity<Client>().HasKey(t => t.Id);
            modelBuilder.Entity<Client>().HasMany(t => t.Contacts).WithOne(c =>c.Client).HasForeignKey(k => k.ClientId);

        }
    }

    /* Db context - Endd */


    public enum ContactType
    {
        Website,
        PhoneNumbers,
        Emails,
        Social
    }

如果您需要更多信息,请与我们联系。

答案 1 :(得分:0)

在DSR的帮助下,这是我的解决方案(未经测试)。

public class Company
{
    public String Id { get; set; }
    public String Name { get; set; }

    public ICollection<ContactPhone> PhoneNumbers { get; set; }
    public ICollection<ContactEmail> ContactEmail { get; set; }
    public ICollection<ContactWebsite> ContactWebsite { get; set; }
    public ICollection<ContactSocial> ContactSocial { get; set; }

}
public class Client
{
    public String Id { get; set; }
    public String Name { get; set; }

    public ICollection<ContactPhone> PhoneNumbers { get; set; }
    public ICollection<ContactEmail> ContactEmail { get; set; }
    public ICollection<ContactWebsite> ContactWebsite { get; set; }
    public ICollection<ContactSocial> ContactSocial { get; set; }
}
 public class ContactWebsite
{
    public String Id { get; set; }
    public String Url { get; set; }

    public Company Company { get; set; }
    public Client Client { get; set; }
}
public class ContactPhone
{
    public String Id { get; set; }
    public String Type { get; set; }
    public String Number { get; set; }

    public Company Company { get; set; }
    public Client Client { get; set; }
}
public class ContactEmail
{
    public String Id { get; set; }
    public String Category { get; set; }
    public String Email { get; set; }

    public Company Company { get; set; }
    public Client Client { get; set; }
}
public class ContactSocial
{
    public String Id { get; set; }
    public String Site { get; set; }
    public String Handle { get; set; }

    public Company Company { get; set; }
    public Client Client { get; set; }
}