Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a poco class with private setter in EntityFramework

What am trying to achieve is to create a class as shown below.

public class Purse
{
    public decimal AvaliableBalance { get; protected set; }        
    public decimal PendingBalance { get; protected set; }
    public decimal TotalBalance { get; protected set; }
}

But am wondering if entity framework will be able to populate this class. If so, how?

Basically what am trying to accomplish is to restrict writing directly to these properties on the code side but i want Entity framework to be able to write to this properties.I want to expose the setter of these properties through a method whose sole purpose is to write values to it (ie: after some processing of course).

like image 617
Cizaphil Avatar asked Feb 02 '15 21:02

Cizaphil


1 Answers

Your approach will work. I have used this pattern heavily. Entity Framework uses reflection to materialize your data into objects. Since reflection will work happily even if your members are private, the pattern works well.

like image 55
Nick Strupat Avatar answered Sep 30 '22 03:09

Nick Strupat