【发布时间】:2019-04-14 21:07:27
【问题描述】:
我有 3 张桌子
用户 > 员工 > 操作员
彼此之间是一对一的关系
public class User
{
[Key]
public int Id { get; set; }
public string Username { get; set; }
public DateTime? LoginDate { get; set; }
public bool IsActive{ get; set; }
public ICollection<Role> Roles { get; set; }
public virtual Employee Employee { get; set; }
}
public class Employee
{
[Key]
[ForeignKey("User")]
public int Id { get; set; }
public string Name{ get; set; }
public string Email { get; set; }
public int UserId { get;set; }
public virtual User User { get; set; }
public virtual Operator Operator{ get; set; }
}
public class Operator
{
[Key]
[ForeignKey("Employee")]
public int Id { get; set; }
public int EmployeeId {get;set;}
public EmployeeEmployee{ get; set; }
}
但是,当我在 Sql Server MS 中创建图表以检查关系时,这确实会创建一对一的关系。
问题是,当我试图直接从 Sql 以图形方式插入数据时,它希望我在 Employee 和 Operator 表上插入主键。为什么它们不像用户那样是自动的?
【问题讨论】:
-
为什么
Id属性具有ForeignKey属性? -
因为这是教程中介绍的如何配置一对一关系entityframeworktutorial.net/code-first/…
标签: c# entity-framework-6