ResiEvil
ResiEvil

Reputation: 21

Add a non mapped property to entity EF5

I'm using EF5 to produce a model from an existing DB structure. I map Insert/Update/Delete Stored Procedures to the entities. This is working fine.

What I would like to do next is pass a UserId to these SPs as a parameter but not have the UserId as a column within the underlying table (the SPs will utilize this parameter). I have lots of entities. Is it possible to somehow add a property that will always be added back in even after updating the model from the DB?

Many Thanks

Upvotes: 2

Views: 172

Answers (2)

Luca Perotti
Luca Perotti

Reputation: 196

Entity created by EF are partial class so you can extend that class with your custom properties

YourEntity.cs //created by EF

public partial class YourEntity
{

    public string Name { get; set; }
    ...
}

YourEntityExtended.cs // created by you

public partial class YourEntity
{

    public int Id { get; set; }       
}

Upvotes: 1

Tetsujin no Oni
Tetsujin no Oni

Reputation: 7367

If you are using EDMX to generate the Entity Framework object model, your classes are partial and you can add properties to the partial classes of your entities which will survive database regeneration.

If you have a ParticularEntity table in the DB and referenced in the EDMX, you may add a partial class file ParticularEntity.Augments.cs (the name is for your reference, and you can have multiples as normal with partial classes) to your project and within it

public partial class ParticularEntity
{
    public string UserId { get; set; }
    public void DoSomething(string userId)
    {
        someFunctionThatYouWantToNotBeAnExtension();
    }
}

Alternatively, you could write a set of extension methods which your maps utilize. I don't think that's as clean as extending the EF classes with partials, though.

Upvotes: 1

Related Questions