c# - 实体类型需要定义主键
问题描述
我有 3 个模型类 ( Customer, Manager, Technician
) 从基类继承Person
。Id
键在基类中定义Person
。
当我尝试为Customer
该类生成控制器时,会显示一个错误,指示实体类型客户必须具有主键。
这是我的Person
课:
public class Person
{
[Key]
public int Id { get; }
[EmailAddress]
public string? Email { get; set; }
public int? Cin { get; set; }
public string? Address { get; set; }
[Required]
public string Name { get; set; }
[Phone, Required]
public int PhoneNumber { get; set; }
public Person(int PhoneNumber, string Name, string Email = null, int? Cin = null, string Address = null)
{
this.PhoneNumber = PhoneNumber;
this.Name = Name;
this.Email = Email;
this.Address = Address;
this.Cin = Cin;
}
public Person()
{
}
}
这是我的Customer
课:
public class Customer : Person
{
public List<Device> CustomerDevices { get; set; }
public Customer(int PhoneNumber, string Name, string Email = null, int? Cin = null, string Address = null)
: base(PhoneNumber, Name, Email, Cin, Address)
{
}
public Customer() : base()
{
}
}
解决方案
您的代码示例中的问题是您应该将set添加到您的Id
属性中,以便实体框架可以设置自动生成的 id。
推荐阅读
- pandas - 将 CSV 列数据提取到单个 Numpy 数组
- grafana - 在 Grafana InfluxDB 中选择字符串 FROM
- javascript - 如何将 css 模块生成的类传递给 Vue 中的组件
- c# - 如何让控制台读取用户输入的内容数量?
- python - 如果 pyenv 设置为针对多个版本的 python 运行,如何运行 python 代码
- c - 找出子网掩码
- bash - 如何在 shell/bash 脚本中读取 json 对象?
- arrays - 如何通过列表
到列表 来自 JSON? - python - Left += 字符串操作
- r - 如何按字母顺序列出 R 数据框变量?