2013-01-21 132 views
1

我正在使用Entity Framework和Mysql。我有每個表的實體類。如何擴展實體類?

但我不知道如何將字段擴展到未在DB中定義的實體類

例如)我有測試表和表有ID,價格和數量字段。

我的實體類是這樣的,

[Table('test')] 
public class Test 
{ 
    [Key] 
    public int id {get; set;} 
    public decimal price {get; set;} 
    public int qty {get; set;} 
} 

現在,我需要在測試類共計外勤。 (出於某種原因,我不能修改DB

所以我儘量使測試類的部分類,

[Table('test')] 
public partial class Test 
{ 
    [Key] 
    public int id {get; set;} 
    public decimal price {get; set;} 
    public int qty {get; set;} 
} 

public partial class Test 
{ 
    public decimal? subTotal {get; set;} 
} 

然後,我得到了一個錯誤:它說,「未知列」 Extent1 'subTotal'in'field list''

有人知道,我怎樣才能將subTotal字段添加到Test類中而不改變數據庫結構?

請指教我。

回答

3

使用NotMappedAttribute表示想要在模型中使用的任何屬性,但不希望Entity Framework映射到數據庫。

[Table('test')] 
    public class Test 
    { 
     [Key] 
     public int id {get; set;} 
     public decimal price {get; set;} 
     public int qty {get; set;} 
     [NotMapped] 
     public decimal? subTotal {get; set;} 
    } 
+0

非常感謝! –