Adding additional properties to the Entity Framework Model

.net c# entity-framework


Please forgive me if this is a rookie question since I'm new to EF.

For our "platform" project, we basically have an EF model built up using Model First that is shared across numerous apps that we develop on top of this platform. In some of these apps, we wish to add new attributes to the classes without altering the platform's model. Does EF 4 allow for this, and if so, how can I go about doing it without changing the.edmx file?

I see that all of the created classes are partial, so I may be able to make a new partial class with the same name and add the additional attributes, but are there any mappings that I should be concerned about?

P.S. Normally, I'd have liked to utilize inheritance and design a new class to house the additional properties, but with EF, I'm not sure how to achieve that. If you can shed any light on this, please do so!

Thank you,

5/23/2011 3:41:43 PM

Accepted Answer

You cannot utilize inheritance since EF won't be aware of inheritance after the entity has been imported from the data source, and as a result, it will instantiate a base type without your properties rather than a derived type with them. Any inheritance that EF needs to operate with must be mapped in EDMX.

Your issue will be resolved by using partial class, but:

  • Partial class components must all be declared in the same assembly.
  • Your partial part's properties are not saved to the database.
  • You cannot utilize properties from your partial portion in linq-to-entities searches.
5/23/2011 4:15:26 PM

Popular Answer

Partial classes are created by EF. As a result, to extend MyEntity, create a MyEntity.cs file containing

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

the same namespace as your produced entities, modify

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow