在EF Code First中,是否可以封装应保留在数据存储中的属性? [英] In EF Code First is it possible to encapsulate properties that should be persisted to the data store?

查看:75
本文介绍了在EF Code First中,是否可以封装应保留在数据存储中的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,假设我有一个POCO,我希望EF Code First可以持久保存到数据存储中:

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;
        }
    }
}

现在,无论出于何种原因,我不希望在对象外部访问 AmountRaised 属性,但是我 do 希望它持久保存到数据存储中。

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?

推荐答案

您可以将其设置为内部,并确保包含POCO /域对象的项目与 DbContext 类位于同一程序集中。或者,您可以在包含POCO的文件中声明朋友程序集:

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)]

这篇关于在EF Code First中,是否可以封装应保留在数据存储中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆