9

I'm new to EF so please excuse me if this is a noob question.

Basically, we have a EF model set up using Model First for our 'platform' project and is shared across many applications which we build on top of this platform. In some of these applications we want to extend the classes to include additional properties without changing the model in the platform. Is this possible with EF 4 and how would I be able to do it without modifying the .edmx file?

I notice that the generated classes are all partial so potentially I could create a new partial class with the same name to include the new properties but is there any mappings that need to be taken care of?

p.s. under normal circumstances I'd have preferred to use inheritance and create a new class to hold the new properties instead but again, I don't know how to do that with EF.. any enlightenment here will be much appreciated!

Many thanks,

theburningmonk
  • 15,701
  • 14
  • 61
  • 104

3 Answers3

25

You cannot use inheritance because once entity is loaded from the data source EF will not know about inheritance and because of that it will instantiate base type without your properties instead of derived type with your properties. Any inheritance must be mapped in EDMX if EF have to work with it.

Using partial class will solve your problem but:

  • All parts of partial class must be defined in the same assembly
  • Properties from your partial part are not persisted to the database
  • Properties from your partial part cannot be used in linq-to-entities queries
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • thank you, that pretty much answered everything I wanted to know – theburningmonk May 23 '11 at 16:28
  • "must be defined in the same assembly" helped me. i'd really like to extend the entity based on the domain which it is being included in.. without using something like automapper for the binding across layers. – hanzolo Feb 16 '17 at 17:45
8

EF generates partial classes. So to extend MyEntity, create a MyEntity.cs file with

partial class MyEntity
{
    public string MyExtraProperty {get;set;}
}

edit: in the same namespace as your generated entities

Kaido
  • 3,383
  • 24
  • 34
  • but I can't create a partial class in a separate project, any way to get around it? – theburningmonk May 23 '11 at 16:04
  • You can change the template to derive from Your class own class, which class can be in another project, but the base class must derive from EntityObject. – Akash Kava May 23 '11 at 16:19
  • What is the extension for? It may be prudent to use inheritance if you require a local extension for UI or similar (Or alternatively map to a viewmodel). If you require an application wide extension then why would you use a separate project? – Kaido May 23 '11 at 16:26
1

I agree with adding additional properties to partial class of your entities (as you and Kaido said).

This way you can freely add the properties you want, without modifying generated classes and if you generate your model again (or update it from DB), your partial class is not modified.

In my opinion, adding properties to partial classes of generated entities is the way to go.

Afshin Gh
  • 7,918
  • 2
  • 26
  • 43