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


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

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

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 ?

Thank you!

EDITED FOR : If your model is a representation of your database and in the database you don't have the field, why do you want to add it manually?


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

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


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

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

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)

//return data

I hope it could explain the question :)

Thanks again.

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