c# - 无法设置 EF 代码优先类的 PK

标签 c# .net entity-framework

我有一个 Device 类,它的主键是它的序列号 ala:

public class Device
{
    /// <summary>
    /// Device serial number
    /// </summary>
    [Key]
    public int Serial { get; set; }
}

出于某种原因,当我第一次创建此类时, Entity Framework 实际上不会让我设置主键。相反,它为 Serial 创建了自己的编号。我错过了什么?

context.Devices.AddOrUpdate(new Device()
{
    Serial = 89484848 // never gets set, EF sets its own PK
});

最佳答案

我认为您应该将键设置为 DeviceIdId 而不是 Serial(这与表名不同)

你可以看看这个链接 msdn blog入口

关于c# - 无法设置 EF 代码优先类的 PK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18190466/

相关文章:

.net - 如果您必须创建类似 ReSharper 的东西,您会从哪里开始?

entity-framework - 导出大型 Entity Framework 图

c# - Linq IQueryable 通用筛选器

c# - 右对齐打印文本

c# - 包含 Unicode 字符检查失败

c# - 如何在不知道更新了哪些字段的情况下处理更新实体?

c# - 参数 5 : cannot convert from 'System.Drawing.Image' to 'string' - calling class1 from Main Class

c# - 如何在 C# 中分段下载文件?

c# - 如何使用自动映射器有条件地将目标对象设置为 null

引用类型的 C# 7 ref 返回