将 EF Core 列/字段指定为只读 [英] Specify EF Core column/field as read only

查看:23
本文介绍了将 EF Core 列/字段指定为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SQL Server 表,其中包含由数据库通过默认值设置的某些字段,一旦保存,就应该不再再次修改(例如 DateCreated).

I have a SQL Server table with certain fields that are set by the database via default values that, once saved, should never been modified again (e.g. DateCreated).

在 Entity Framework Core 2.1 模型构建器或类中,我们如何将字段标记"为本质上是只读的?换句话说,我不希望任何代码能够设置或覆盖这些字段.

In the Entity Framework Core 2.1 model builder or classes, how do we "mark" a field as essentially read-only? In other words, I don't want any code to be able to set or overwrite these fields.

根据我的搜索,我会在 .Property() 的末尾添加 .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) 吗?

Based on my searching, would I add .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) at the end of .Property()?

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Doohicky>(entity =>
    {
        ... // other fields

        entity.Property(e => e.DateCreated).HasDefaultValueSql("(getdate())");

        ... // other fields
    });
}

或者我是否在 DateCreated 字段中添加一个 [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 注释?

Or do I add a [DatabaseGenerated(DatabaseGeneratedOption.Identity)] annotation to the DateCreated field?

public class Doohicky
{
    public DateTime DateCreated {get; set;}
}

或者有其他完全的方法吗?

Or is there another way entirely?

我希望将来如果有人决定写这样的东西,就会抛出错误.

I want it such that in the future, if anybody decides to write something like this, an error would be thrown.

model.DateCreated = new DateTime();
dbContext.SaveChanges() // errors out

任何见解将不胜感激.

推荐答案

EF Core 的预期方法是设置 AfterSaveBehavior 属性为默认值以外的值 保存:

The EF Core intended way is to set AfterSaveBehavior property to value other than the default Save:

获取一个值,该值指示在实体保存到数据库后是否可以修改此属性.

Gets a value indicating whether or not this property can be modified after the entity is saved to the database.

如果 Throw,那么当实体存在于数据库中后,如果给该属性分配了新的值,则会抛出异常.

If Throw, then an exception will be thrown if a new value is assigned to this property after the entity exists in the database.

如果 忽略,那么对数据库中已经存在的实体的属性值的任何修改都将被忽略.

If Ignore, then any modification to the property value of an entity that already exists in the database will be ignored.

目前还没有专用的 fluent API,因此您需要直接通过可变属性元数据进行设置,如下所示:

There is no dedicated fluent API yet, so you need to set it directly through mutable property metadata like this:

entity.Property(e => e.DateCreated)
    .HasDefaultValueSql("(getdate())")
    .Metadata.AfterSaveBehavior = PropertySaveBehavior.Throw; // <-- 

更新(EF Core 3.x):从 EF Core 3.0 开始,许多类似的属性已被替换为 Get/Set扩展方法对,所以现在相关代码如下:

Update (EF Core 3.x): Starting with EF Core 3.0, many properties like this have been replaced with Get / Set extension method pairs, so the relevant code now is as follows:

    .Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Throw); 

这篇关于将 EF Core 列/字段指定为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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