我一直在尝试弄清楚应该如何首先建立这两个模型代码之间的关系。我需要建立两个关系:
现在我的问题是,当我创建新分支时,即使联系人确实存在于数据库中,我也会受到外键限制。
public class Branch
{
public Guid Id { get; set; } = Guid.NewGuid()!;
public string BranchName { get; set; } = default!;
[ForeignKey("Id")]
public Guid? ContactPersonId { get; set; } = default!;
public User? ContactPerson { get; set; } = default!;
public DateTime Created { get; set; } = DateTime.Now;
[ForeignKey("BranchId")]
public ICollection<User>? Users { get; set; } = new List<User>();
}
用户类别
public class User
{
public Guid Id { get; set; } = Guid.NewGuid()!;
public string FullName { get; set; } = default!;
public string Email { get; set; } = default!;
public string PhoneNumber { get; set; } = default!;
public string? Password { get; set; }
public string? Salt { get; set; }
public UserRoles Role { get; set; }
public Guid? BranchId { get; set; }
}