【发布时间】:2012-05-04 03:28:03
【问题描述】:
我需要在模型中添加一个字段,而数据库实际上没有该字段。
因为,首先我尝试只将字段添加到实体类中。
public partial class Weborder
{
(Auto Generated)
public int orderno {get; set;}
.
.
.
(Add Manually)
public string newField1 {get; set;} //this is new field that DB does not have
public string newField2 {get; set;} //this is new field that DB does not have
}
之后,当我更新 EDXM 时,EDMX 会删除新字段,因为数据库没有该字段。 :(
所以我手动将该字段添加到 EDMX 模型中。 (添加 -> 标量属性)
然后在编译时出现错误,错误消息说:
Error 1 Error 3004: Problem in mapping fragments starting at line 399:No mapping specified for properties ...
An Entity with Key (PK) will not round-trip when:...
有人知道如何在实体类中添加新字段吗?
谢谢!
编辑用于: 如果你的模型是你的数据库的表示,而数据库中没有字段,为什么要手动添加?
=>
检索数据时,对象的返回类型为实体类。
在将数据从控制器传递到视图之前,我需要将更多数据(字段)添加到 IQueryable 结果中。
例如)
public DbSet<WEBORDERLN> WEBORDERLNs { get; set; }
//repository
public IQueryable<WEBORDERLN> WebOrderLns
{
get { return context.WEBORDERLNs; }
}
现在我在控制器中获得了 weborderln 数据。在通过视图之前,我需要
在结果中添加额外的数据。
var data = webOrderLnRepository.WebOrderLns.Where(e => e.PICKNO == OrderNo).ToList();
foreach (WEBORDERLN weborderln in data)
{
weborderln.[NEW FIELD] = "EXTRA DATA"; //// FOR THIS, I NEED TO ADD NEW FILED INTO ENTITY CLASS
}
//return data
我希望它可以解释这个问题:)
再次感谢。
【问题讨论】:
-
如果你的模型是你的数据库的表示,而在数据库中你没有字段,为什么要手动添加呢?
-
@Dante,感谢您的关注。我再次编辑了我的问题,请检查我的问题。如果您有任何想法,请给我建议。
-
您基本上是在描述使用 ViewModel 的场景——也就是说。从数据模型映射并从视图派生的模型,用于将数据传递到(并可能从)视图。现在我一直想知道在实体框架本身中构建 ViewModel 有多么容易(因为我可能想使用和查询 OData 服务等),但我不知道它们是否真的属于那里。
标签: asp.net asp.net-mvc entity-framework entity-framework-4 edmx