2017-07-17 4 views
0

私はコンタクトインフォメーションストラクチャのモデリングに取り組んでおり、リレーションシップをEFコアでどのようにコーディングするかをあまり理解していません。私は、データアクセス層にEFを使用することにかなり新しいです。Asp Core複数のエンティティリレーションシップ

ウェブサイト、電話番号、メールアドレス、または社会情報を含むことができる連絡先モデルが必要です。連絡先情報がいくつかの異なるモデルに追加されます。任意の提案が役立つだろう、私はどのように多くのテーブルの関係で多くのこの1つをコードするか、または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テーブルはすでに1対多の関係にあるため、削除する必要があります。私はそれのためのコードサンプルを与えることができることを教えてください。 – DSR

+0

それは正しいです、それは素晴らしいでしょう。 –

+0

EFコア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の助けを借りて、これは私が(テストされていない)解決策です。そして「クライアント:連絡先」テーブル:私はあなたが「お問い合わせ会社」との多くの関係に1を作成したい理解して何

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; } 
} 
関連する問題