Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架可以设置的只读属性_C#_Entity Framework - Fatal编程技术网

C# 实体框架可以设置的只读属性

C# 实体框架可以设置的只读属性,c#,entity-framework,C#,Entity Framework,非常基本,但我的谷歌fu让我失望 我有一个域对象,比如User,我希望活动属性是只读的。但是,当从数据库加载时,需要通过EF(6)设置属性 表: CREATE TABLE Users ( ID INT NOT NULL PRIMARY KEY CLUSTERED, Active BIT NOT NULL DEFAULT 1 -- other stuff ); 类别: public class User { public int ID { get; set; }

非常基本,但我的谷歌fu让我失望

我有一个域对象,比如User,我希望活动属性是只读的。但是,当从数据库加载时,需要通过EF(6)设置属性

表:

CREATE TABLE Users (
    ID INT NOT NULL PRIMARY KEY CLUSTERED,
    Active BIT NOT NULL DEFAULT 1
    -- other stuff
);
类别:

public class User
{
    public int ID { get; set; }
    public bool Active { get; set; }    // this should be RO

    public void Activate() {
        Active = true;
        this.AddChangelog(ChangeType.Activation, "Activated");
    }

    public void Deactivate() {
        Active = false;
        this.AddChangelog(ChangeType.Activation, "Deactivated");
    }

    // changelogging, etc.
}
开发人员不应直接更改
Active
,而应使用
Activate()
Deactivate()
方法。然而,EF6需要直接设置
Active
,以便实例化对象

架构(如果重要):

  • 用户类存在于
    项目中
  • EF6类配置通过
    Data
    project完成
  • EF6使用Fluent API配置(将ORM内容排除在我的域项目之外)
  • EF6不使用DTO,但直接配置域类
问题

我如何强制(或至少警告)开发人员不要设置
user.Active
?至少,我可以向setter提供一些编译时警告(类似于
[过时]
),以便他们得到通知吗


谢谢,

您可以使用好友程序集来解决您的问题,并将其公开给您想要的项目,并将其公开给其他项目

[assembly:InternalsVisibleTo("cs_friend_assemblies_2")]

我很高兴能帮上忙:)