2017-05-31 1 views
0

私は、SQL Serverの奇妙なレガシーテーブルを持っていると私は次のロジックを使用できるかどうか、私は疑問に思う:エンティティフレームワーク|一つのテーブルから複数のオブジェクト

Table [PartiesTable] 
- Id 
- FirstName 
- LastName 
- Name 
- Type 

そして、次のクラス:

public abstract class Party { 
    public Guid Id { get; set; } 
} 

public class Person : Party { 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

public class Company : Party { 
    public string Name { get; set; } 
} 

そして、これがありますレコードの例

╔════╦═════════════╦══════════╦═══════╦══════╗ 
║ id ║ FirstName ║ LastName ║ Name ║ Type ║ 
╠════╬═════════════╬══════════╬═══════╬══════╣ 
║ 1 ║ John  ║ Kenedy ║ NULL ║ P ║ 
╠════╬═════════════╬══════════╬═══════╬══════╣ 
║ 2 ║ Meresa  ║ Oslo  ║ NULL ║ P ║ 
╠════╬═════════════╬══════════╬═══════╬══════╣ 
║ 3 ║ NULL  ║ NULL  ║ ACME ║ C ║ 
╚════╩═════════════╩══════════╩═══════╩══════╝ 

私はここ(https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application)ドキュメントに従うことを試みたが、彼らはあなたが3 TABLを持っている例を参照してください。私は持っていません。

答えて

2

これは、実際にはEFコードの最初の動作です。単純にクラスを定義し、それらのすべてをPartiesTableという名前のテーブルにマップし、discriminatorカラムを設定します。設定は次のようになります。

modelBuilder.Entity<Party>() 
      .Map<Person>(m => { m.Requires("Type").HasValue("P"); m.ToTable("PartiesTable");}) 
      .Map<Company>(m => { m.Requires("Type").HasValue("C"); m.ToTable("PartiesTable"); }) 
      .ToTable("PartiesTable"); 
+0

@ akos-nagyのスポット、ありがとうございます – Raffaeu

関連する問題