alex
alex

Reputation: 720

Is possible to have a property on model with no corresponding field on database

I'm using Entity framework , with Database First.

I have created the Model with wizard from an existing database.

Now I have this situation :

Let's suppose that I have a table in database :

MyTable1 ( ID , Name , Value1 )

Off-course when I create the model with wizard , on the Model.edmx , is created an entity with the same structure. But for several reasons during the program executions , on the model only I want to have an extra property that has no corresponding field on database.

So on the model I want something like this :

MyTable1(ID , Name,Value1, ExtraValue)

I need this extra property only for some actions during runtime.

Is possible to do this ?

Upvotes: 0

Views: 46

Answers (1)

Jenish Rabadiya
Jenish Rabadiya

Reputation: 6766

Create partial class and annotate property with NotMappedAttribute.

While edmx generates code from your datatbase it creates partial classes for every table. You can add partial class there for your table and then add your property with NotMapped attribute.

For example you are having Student table and you want to add Age property that is not in database then you should do something like following.

public partial class Student
{
    [NotMapped]
    public int Age{get; set;}
}

for more information on NotMappedAttribute visit MSDN documentation here.

Update

for more information on partial classes visit here

You would also like to visit this link Extending Entity Framework Generated Types and Entity Framework using Partial classes to add business logic and validation to generated entities

Upvotes: 1

Related Questions