0

I am using the Entity Framework with a database first approach.

I have used partial classes to successfully add custom methods, but have found that if I add a custom field it is not persisted when my entity is next pulled from the DataContext (entity is not submitted to database in the meantime). I need to use this field for processing but I do not need it, or want it, stored in my database.

public partial class MyEntity
{           
    public decimal MyCustomField;
}

Why is this? How can I get around it?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
waxingsatirical
  • 584
  • 1
  • 7
  • 11
  • you're using database first approach, then update the database to add the column, `alter table add column .. etc`, then go to the edmx diagram, right click, _Update Model from database_ – Yuliam Chandra Sep 18 '14 at 09:24
  • Thanks for your comment, although I did mention in my question that I do not want to store this field in my database. So this is not an appropriate answer. – waxingsatirical Sep 18 '14 at 09:33
  • it's not an answer, it's a comment, you don't want to store this field in the database, you already do that, fields don't get saved into database, only public properties (that is mapped in the related SSDL) – Yuliam Chandra Sep 18 '14 at 09:40
  • Regarding your comment _"The values are persisted in the DataContext until changes are submitted to the database. This is all the persistence that I want"_ - that is true, until an entity gets reloaded from the database. Then your non-mapped property modifications will be lost and reset to their default. Please show relevant code and flow so we can answer your actual question. – CodeCaster Sep 18 '14 at 10:44
  • see http://stackoverflow.com/questions/14768863/ef-5-0-model-first-how-to-make-not-mapped-properties – StefanG Sep 18 '14 at 11:39

2 Answers2

1

One thing you should keep in mind is that Entity Framework do not store values in itself, it just generates SQL queries using its knowledge of database from EDMX. Persistence is actually implemented at the database side.

Yes, you can add custom methods and properties to model using partial classes, but since, you are not creating a database column, the values in those properties will not be persisted and hence the behavior you are getting is perfectly normal.

I hope this helps.

Amit Mittal
  • 1,129
  • 11
  • 30
  • This is not true. EntityFramework does persist values. The values are persisted in the DataContext until changes are submitted to the database. This is all the persistence that I want. – waxingsatirical Sep 18 '14 at 09:45
  • EF is not persisting any values, however, if you make changes to any object, and you want to access those changes at some other function without submitting them to db, you need to pass the same object to that function. – Amit Mittal Sep 18 '14 at 09:53
  • EF is just an ORM, so Object Oriented rules are intact in EF also. – Amit Mittal Sep 18 '14 at 09:55
0

Try Property instead of field.

public decimal MyCustomField{get; set;}
Ronak Patel
  • 2,570
  • 17
  • 20