Entity Framework 4 (using EDMX), how to add a field into to model that DB does not have the field actually

谁说胖子不能爱 提交于 2019-11-30 18:45:47

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.

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;} 
}

Dosn't [NotMapped] work.

 [NotMapped]
 public string newField1 {get; set;}

First of all, you shouldn't modify the data model file. This file represents your data.

Second, you shouldn't be returning your data model objects/collections from your Repository. This is a very bad practice because you are creating a dependency between the Controller/View and the Model. I suggest you create custom Model objects that contain the properties you need in your View, map your entities to those Model objects and only return Model objects or collections of Model objects from your Repository.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!