【发布时间】:2020-12-25 18:26:37
【问题描述】:
我正在尝试创建一个包含属性的抽象类,并且我的解决方案中的每个实体都将继承此属性。
我确实希望对抽象类属性进行任何用户修改,但代码所做的更改除外。
我想要达到的目标:
public abstract class SystemEntityBase
{
/// <summary>
/// Gets the date when entity was created.
/// </summary>
public DateTime Created { get; } = DateTime.UtcNow;
/// <summary>
/// Gets the date when entity was last active in the system.
/// </summary>
public DateTime LastActive { get; } = DateTime.UtcNow;
}
因为我使用的是只读属性,所以 EntityFrameworkCore 在自动生成迁移脚本时不会将此字段添加到我的数据库表中。
我很好奇在我的情况下,在创建数据库列的同时限制属性的可能解决方案是什么?
【问题讨论】:
-
[ReadOnly(true)]有帮助吗? -
你可能还喜欢this approach。
标签: c# entity-framework .net-core entity-framework-core