2017-08-01 94 views
1

我有這樣的場景:實體框架外鍵的多個表的映射

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

在Table 1和Table屬性applicationCode必須是關鍵,因爲我可以有不同的應用程序相同的代碼。

在Table3中我引用了Table1和Table2。如何爲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; } 
} 

的ApplicationCode屬性表3中可以rapresent爲表1和表2中同時外鍵?

+0

我新的EF ...嘗試使用數據標註 '[重點,ForeignKey的( 「Table1Object」),爲了= 1]'' [鍵,ForeignKey的( 「Table2Object」,order = 2)]'。如果它正確告訴我發佈答案 –

+0

嗨@dim mik但項目不建立! 謝謝你的 – Marco

+0

這是有點臭:)我要麼與設計(介紹一個ApplicationTable1接口表)或使用[繼承]多個對多(https://docs.microsoft.com/en-us/aspnet/ mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementation-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application)以Application作爲鑑別器。 –

回答

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