gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:09:03 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com