【问题标题】:Asp Core Multiple Entity RelationshipsAsp Core 多实体关系
【发布时间】:2017-07-18 17:17:51
【问题描述】:

我正在对联系信息结构进行建模,但还没有完全弄清楚应该如何使用 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;}
 }

【问题讨论】:

  • 我了解您希望与“公司:联系人”和“客户:联系人”表建立一对多关系。那正确吗?然后您需要删除 ContactInfo 表,因为它本身已经是一对多关系。让我知道我可以给你代码示例。
  • 没错,那太棒了。
  • 这将很容易使用 EF Core 2.0 中的自有实体类型进行映射

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


【解决方案1】:

如果我正确理解了您的问题,那么您可以使用以下代码示例,但这并不完全是您想要实现的目标。这可能会让您了解您需要使用 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
    }

如果您需要更多信息,请告诉我。

【讨论】:

  • 在数据库中,contact 会有一个 CompanyId 和 ClientId 列。对于公司联系人,ClientId 将为 0,对于客户联系人,CompanyId 将为 0,对吗?
  • 使那些 FK 可以为空。我用“?”修改了答案。因此,它将为 NULL。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    相关资源
    最近更新 更多