2017-07-17 118 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; } 
} 
相關問題