Search code examples
entity-frameworkdatabase-first

How to solve field wrapping in Entity Framework database-first


When I use database first, after creating the edmx file, all the conceptual models have already been generated. But I want to do some special operations on certain fields. For example, there's a field named 'price'; I want the matching property 'Price' to return double of the 'price'. How can I do that? If I modify the getter in the code, every time I update the model from database, all of the modifications go away.

What's the correct way to do this?


Solution

  • What you can do is create a partial class for entity which contains the Price Property and put a getter like this (A property with double price will be meaningful ),

    Public partial class YourEntity{
      Public float DoublePrice{
        get { return Price*2;}
     }
    }
    

    Or you can create a class inherited from the entity,

     Public partial class Entity:YourEntity{
          Public override float Price{
            get { return base.Price*2;}
         }
      }