anIBMer
anIBMer

Reputation: 1209

Stop Entity Framework to create some properties in model

I am trying to use Model first Entity Framework in MVC4. Would like to disable the creation of some properties in the entity model, these properties only suppose to be used as a viewModel. And I will populate the model properties in the controller dynamically.

May I know what annotation attribute I should put for these properties?

If this cannot be done, then I must create a separated ViewModel to do this. However, the view model will still have other properties linked with the entity model, what is the best way to map them together? Thanks.

Upvotes: 0

Views: 874

Answers (2)

Jeric Cantos
Jeric Cantos

Reputation: 278

You may want to try the [NotMapped] Attribute to tell EF not to save a property. Something like:

public class MyEntity {
     public int Id {get; set;} // will be stored as a column in the DB
     [NotMapped]
     public int MyProperty {get; set;} // will not be stored as a column in the DB
}

Upvotes: 2

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

Entity framework designer creates model of persisted entities. All properties added through this designer are persisted. The designer creates partial class for every modeled entity. If you want to have additional non-persisted properties available for your views you can either create your own partial part of the class for an entity with only non-persisted properties (persisted properties are already part of the autogenerated part) or you can create specialized view model with all properties you need.

Upvotes: 1

Related Questions