In EF Code First is it possible to encapsulate properties that should be persisted to the data store?

余生长醉 提交于 2019-12-13 17:25:11

问题


So let's say I've got a POCO that I want EF Code First to persist to a data store:

public class Campaign
{
    public string Name { get; set; }
    public double GoalAmount { get; set; }
    private double AmountRaised { get; set; }
    public bool GoalMet
    {
        get
        {
            return AmountRaised >= GoalAmount;
        }
    }
}

Now, for whatever reason, I don't want the AmountRaised property to be accessible outside the object, but I do want it persisted to the data store. Is this possible with EF Code First?


回答1:


You can make it internal and make sure that the project that contains the POCOs / domain objects are in the same assembly as your DbContext classes. Or you can declare "friend" assemblies in the files that contain your POCOs:

[assembly: InternalsVisibleTo("MyOther.Assembly")]



来源:https://stackoverflow.com/questions/4731346/in-ef-code-first-is-it-possible-to-encapsulate-properties-that-should-be-persist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!