2017-08-01 14 views
1

私はこのシナリオがあります:表1にEntity Frameworkの外部キーの複数のテーブルマッピング

public class Application 
{ 
    [Key] 
    public string Code { get; set; } 
} 

public class Table1 
{ 
    [Key] 
    public string Table1Code { get; set; } 

    [Key, ForeignKey("ApplicationObject")] 
    public string Application { get; set; } 
    public virtual Application ApplicationObject { get; set; } 
} 

public class Table2 
{ 
    [Key] 
    public string Table2Code { get; set; } 

    [Key, ForeignKey("ApplicationObject")] 
    public string Application { get; set; } 
    public virtual Application ApplicationObject { get; set; } 
} 

public class Table3 
{ 
    [Key, ForeignKey("Table1Object")] 
    public string Table1Code { get; set; } 
    public virtual Table1 Table1Object { get; set; } 

    [Key, ForeignKey("Table2Object")] 
    public string Table2Code { get; set; } 
    public virtual Table2 Table2Object { get; set; } 

    [Key, ForeignKey("Table1Object")] 
    public string ApplicationCodeTab1 { get; set; } 

    [Key, ForeignKey("Table2Object")] 
    public string ApplicationCodeTab2 { get; set; } 
} 

をし、私は別のアプリケーションのために同じコードを持つことができるため、プロパティのapplicationCodeはキーでなければならない表2。

表3では、私は表1と表2を参照しました。プロパティを重複させずにApplicationCodeプロパティに外部キーを追加するにはどうすればよいですか?例については

public class Table3 
{ 
    [Key, ForeignKey("Table1Object")] 
    public string Table1Code { get; set; } 
    public virtual Table1 Table1Object { get; set; } 

    [Key, ForeignKey("Table2Object")] 
    public string Table2Code { get; set; } 
    public virtual Table2 Table2Object { get; set; } 

    [Key] 
    public string ApplicationCode { get; set; } 
} 

表3でApplicationCodeプロパティは、同じ時間に表1と表2のための外部キーをrapresentことができますか?

+0

私はEFに新しいです... [= 1のForeignKey( "Table1Object")、キー順] ' ' [キー、ForeignKeyの(データ注釈 を '使用するようにしてください"Table2Object"、order = 2)] 'となります。その正しい場合は、回答を投稿するように教えてくれる –

+0

こんにちは@dim mikがプロジェクトをビルドしていない! ありがとう – Marco

+0

それは匂いのようなものです:)私はデザイン(ApplicationTable1ジャンクションテーブルを導入する)または[継承](https://docs.microsoft.com/en-us/aspnet/)を使用して多対多に行きます。 mvc/overview/getting-started-with-ef-using-mvc /実装継承 - エンティティ - フレームワーク - as-asp-net-mvcアプリケーション)をdiscriminatorとして使用します。 –

答えて

2

キーは、ForeignKey属性をナビゲーションプロパティに移動し、外部キー列を指定することです。このよう

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Data.Entity; 
using System.IO; 
using System.Linq; 
using System.Threading.Tasks; 

namespace ConsoleApp6 
{ 

    public class Application 
    { 
     [Key] 
     public string Code { get; set; } 
    } 

    public class Table1 
    { 
     [Key] 
     [Column(Order = 0)] 
     public string Table1Code { get; set; } 

     [Key, ForeignKey("ApplicationObject")] 
     [Column(Order = 1)] 
     public string Application { get; set; } 
     public virtual Application ApplicationObject { get; set; } 
    } 

    public class Table2 
    { 
     [Key] 
     [Column(Order = 0)] 
     public string Table2Code { get; set; } 

     [Key, ForeignKey("ApplicationObject")] 
     [Column(Order = 1)] 
     public string Application { get; set; } 
     public virtual Application ApplicationObject { get; set; } 
    } 

    public class Table3 
    { 
     [Key] 
     [Column(Order = 0)] 
     public string Table1Code { get; set; } 

     [Key] 
     [Column(Order = 1)] 
     public string Table2Code { get; set; } 

     [Key] 
     [Column(Order = 2)] 
     public string ApplicationCode { get; set; } 

     [ForeignKey("Table1Code,ApplicationCode")] 
     public virtual Table1 Table1Object { get; set; } 

     [ForeignKey("Table2Code,ApplicationCode")] 
     public virtual Table2 Table2Object { get; set; } 



    } 
    class Db: DbContext 
    { 


     public DbSet<Table1> Table1 { get; set; } 
     public DbSet<Table2> Table2 { get; set; } 
     public DbSet<Table3> Table3 { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.OneToManyCascadeDeleteConvention>(); 
      base.OnModelCreating(modelBuilder); 
     } 

    } 
    class Program 
    { 

     static void Main(string[] args) 
     { 


      Database.SetInitializer(new DropCreateDatabaseAlways<Db>()); 

      using (var db = new Db()) 
      { 
       db.Database.Log = m => Console.WriteLine(m); 

       db.Database.Initialize(true); 


      } 

      Console.ReadKey(); 

     } 
    } 
} 
+0

いい@David Browne ....あなたの解決策はうまくいきます! – Marco

関連する問題