11

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.

ex)

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

//repository
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)
{
   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 :)

Thanks again.

JayC
  • 7,053
  • 2
  • 25
  • 41
Expert wanna be
  • 10,218
  • 26
  • 105
  • 158
  • 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? – Dante Apr 23 '12 at 13:57
  • @Dante, Thank you for your concern. I edited my question again, please review my question. And If you have any idea, please advice me. – Expert wanna be Apr 23 '12 at 14:15
  • You're basically describing the scenario for the utilization of ViewModels--that is. models mapped from the data models and derived from the view for the purpose of passing data to (and possibly from) the view. Now I've always wondered how easy it would be to build ViewModels in Entity Framework itself (because I might want to use and query OData Services, etc.) but I don't know that they really belong there. – JayC Apr 23 '12 at 14:19

3 Answers3

14

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;} 
}
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thank you for your answer! could you give me a little more hint about "You must create a new partial part of your entity class (in the new .cs file) and add new fields to that class."? – Expert wanna be Apr 23 '12 at 14:29
  • I create a new class for entity and copy all the original codes into new file (WEBORDERLN2.cs) and also modify context and repository return type object to WEBORDERLN2. but error occur, it say "The entity type WEBORDERLN2 is not part of the model for current context." do you have any idea about this? Thank you! – Expert wanna be Apr 23 '12 at 14:36
  • Not a new class. Do you know what the `partial` keyword mean in C#? It allows you to define one class in multiple files. The class must be the same as the one generated by EF - only the generated code is in different file than your custom properties. – Ladislav Mrnka Apr 23 '12 at 14:38
  • Now it works! awesome!!!!! N I understand about the 'partial' now. Thank you very much! – Expert wanna be Apr 23 '12 at 14:56
6

Dosn't [NotMapped] work.

 [NotMapped]
 public string newField1 {get; set;}
jrb
  • 1,708
  • 2
  • 13
  • 20
3

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.

Dante
  • 3,833
  • 4
  • 38
  • 55