我有一个 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
});
最佳答案
我认为您应该将键设置为 DeviceId 或 Id 而不是 Serial(这与表名不同)
你可以看看这个链接 msdn blog入口
关于c# - 无法设置 EF 代码优先类的 PK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18190466/