实体框架4(使用EDMX),如何将字段添加到模型的数据库不具备实际领域 [英] Entity Framework 4 (using EDMX), how to add a field into to model that DB does not have the field actually

查看:188
本文介绍了实体框架4(使用EDMX),如何将字段添加到模型的数据库不具备实际领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个字段添加到该数据库不具有字段实际上模型。

I need to add a field into model that Database does not have the field actually.

由于,首先我想现场加入到实体类只。

Because, firstly I tried to add the field into Entity class only.

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删除新的领域,因为数据库没有现场。 (

and later, when I update EDXM then EDMX remove the new fields because the database does not have the field. :(

所以我手动添加字段插入EDMX模型。 (添加 - >标量属性)

So I add the field into EDMX model manually. (Add -> Scalar Property)

然后在编译时出现错误,该错误消息说:

then an error occur while compiling, the error message say :

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:...

有人知道如何添加新字段到实体类?

Anybody know how to add new fields into entity class ?

感谢您!

编辑的:
如果你的模型是数据库的重新presentation并在数据库中你没有外地,你为什么要手动添加呢?

=>

当检索数据,对象的返回类型是实体类。

When retrieve data, the return type of object is the entity class.

和之前从控制器传递数据的查看,我需要更多的数据(场)加入到IQueryable的结果。

and before passing data from controller to view, I need to add more data(fields) into the IQueryable result.

EX)

public DbSet<WEBORDERLN> WEBORDERLNs { get; set; }

//repository
public IQueryable<WEBORDERLN> WebOrderLns
{
      get { return context.WEBORDERLNs; }
}

和现在我得到的控制器weborderln数据。并通过视图之前,我需要

and now I get the weborderln data in controller. and before passing view, I need to

额外的数据加入到结果。

add extra data into the result.

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

我希望这可以解释这样一个问题:)

I hope it could explain the question :)

再次感谢。

推荐答案

您必须创建实体类的一个新的部分零件(新.cs文件),并添加新字段该类。您不得修改由自动生成创建的局部部分,因为自动生成的文件将在每次你改变EDMX文件被覆盖。您还必须不包括EDMX领域,因为EDMX定义你的映射数据库=它包含了数据库领域唯一的

You must create a new partial part of your entity class (in the new .cs file) and add new fields to that class. You must not modify the partial part created by autogeneration because autogenerated files will be overwritten every time you change EDMX file. You also must not include the field in EDMX because EDMX defines your mapping to database = it contains only fields in database.

创建同一程序和命名空间的新文件WebOrderPart.cs为包含你自动生成类:

Create a new file WebOrderPart.cs in the same assembly and namespace as your autogenerated classes containing:

public partial class Weborder
{
  public string newField1 {get; set;} 
  public string newField2 {get; set;} 
}

这篇关于实体框架4(使用EDMX),如何将字段添加到模型的数据库不具备实际领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆