c# - 如何防止 EF Core 3.1 中的列更新?

标签 c# entity-framework-core ef-core-3.0 ef-core-3.1

我从 .Net Core 2.2 升级到 3.1,但此功能已被弃用

modelBuilder
.Entity<Order>()
.Property(e => e.CreationTime)
.ValueGeneratedOnAddOrUpdate()
.Metadata.IsStoreGeneratedAlways = true;


我需要 EF 来执行插入但阻止更新。

谢谢!

最佳答案

据官方documentation , IsStoreGeneratedAlways 从 3.1 开始过时。

Microsoft.EntityFrameworkCore.Metadata Assembly:

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.



你应该尝试这样的事情:
modelBuilder
    .Entity<Order>()
    .Property(e =>.CreationTime).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);

关于c# - 如何防止 EF Core 3.1 中的列更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59977743/

相关文章:

sqlite - 如何在 Azure 上使用 EF 更新/迁移 SQLite 数据库

c# - 仅在 EF Core 中的 Scaffold-DbContext 存储过程

c# - 如何遍历匿名对象列表?

c# - 结构体 "this"的含义 (C#)

c# - 获取项目的内容类型列/元数据

c# - .NET Core/EF.Core 3+ 将控制台日志记录添加到 DbContext

c# - 抑制 EF 核心 3.0.x 初始化消息

c# - 内存中的 CLR 存储过程和对象

sql-server - Entity Framework 7 无法插入日期时间

c# - 使用自定义表达式扩展 EF Core 'where' 子句