2017-07-17 117 views
0

我正在建模联系人信息结构,并没有完全弄清楚如何使用EF Core编码关系。我对使用EF进行数据访问层相当新颖。Asp核心多个实体关系

我想要一个可以包含网站,电话号码,电子邮件或社交信息的联系人模式。然后联系信息将被添加到几个不同的模型。任何建议都会有所帮助,我不知道如何使用许多表关系来编码这个One to many,或者甚至可以使用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;} 
} 
+0

我知道你想与“公司:联系人”和“客户:联系人”表创建一对多的关系。是对的吗?那么你需要删除ContactInfo表,因为它已经在一对多的关系中。让我知道我可以给你代码示例。 – DSR

+0

这是正确的,那会很棒。 –

+0

这将非常容易使用EF Core 2.0中的所有者实体类型进行映射。 – Smit

回答

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 
    } 

让我知道你是否需要任何信息。

+0

在数据库中,联系人将具有CompanyId和ClientId列。对于公司联系人,ClientId将为0,对于客户联系人,CompanyId将为0,对不对? –

+0

使这些FK可以为空。我用“?”修改了答案。所以,它将是NULL。 – DSR

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; } 
} 
相关问题