【发布时间】:2016-05-25 13:37:29
【问题描述】:
如何在实体框架核心rc2中实现typesafe enum pattern?
public class TestStatus
{
[Column("Id")]
public int Id { get; private set; }
[Column("Description")]
public string Description { get; private set; }
[Column("LongDescription")]
public string LongDescription { get; private set; }
private TestStatus(int id
, string description
, string longDescription)
{
Id = id;
Description = description;
LongDescription = longDescription;
}
public TestStatus() { }
public static readonly TestStatus Active = new TestStatus(1, "Active", "Active Long Description");
public static readonly TestStatus Pending = new TestStatus(2, "Pending", "Pending Long Description");
public static readonly TestStatus Cancelled = new TestStatus(3, "Cancelled", "Cancelled Long Description");
}
在 OnModelCreating 中设置 id 生成策略:
builder.Entity<TestStatus>()
.Property(s => s.Id)
.ValueGeneratedNever();
这是一个简化的例子,但真正的代码是在 rc1 中工作的。升级到 rc2 时,我必须添加 Column 属性以便映射属性(我假设这是因为私有 setter)。尝试分配类型安全枚举值时:
var i = new TestItem
{
Name = "Test Item 2",
Status = TestStatus.Active
};
_context.Items.Add(i);
_context.SaveChanges();
根据用例,我会遇到以下错误之一:
InvalidOperationException:无法跟踪实体类型“TestStatus”的实例,因为已在跟踪具有相同键的此类型的另一个实例。对于新实体,请考虑使用 IIdentityGenerator 生成唯一键值。
或者
SqlException:违反主键约束“PK_Statuses”。无法在对象“dbo.Statuses”中插入重复键。重复键值为 (1)。声明已终止。
我理解错误。 EF 认为我正在尝试创建一个具有相同 ID 的新实例。我如何告诉 EF 这些实例应该被视为相同?我可以通过远离类型安全枚举模式来解决这个问题。如果可能的话,我只想让它与模式一起工作。它在 rc1 中工作。
【问题讨论】:
-
为什么
TestStatus对象都具有相同的Id? -
@noox 这是一个错字。我已经编辑了这个问题来解决它。谢谢!
标签: c# .net entity-framework enums entity-framework-core