2013-01-21 88 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

非常感谢! –