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

查看:210
本文介绍了将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.

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

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

还是我添加 [DatabaseGenerated(DatabaseGeneratedOption。身份)标注到 DateCreated 字段?

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

任何见解将不胜感激。 / p>

Any insight would be greatly appreciated.

推荐答案

EF核心的预期方法是设置 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.

如果抛出,如果在数据库中存在该实体后,如果为此属性分配新值,则会引发异常。

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.

尚无专用的流畅的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天全站免登陆