gpt4 book ai didi

c# - Fluent API - 一对多关系

转载 作者:太空宇宙 更新时间:2023-11-03 23:08:53 25 4
gpt4 key购买 nike

我有两个实体,即员工和公司。这两者都可以有一个或多个地址。 由于 Guid 始终是唯一的,所以我想在 Employee 和 Company 中使用 Guid 作为 Address 中的外键。
也就是说,Address for Employee 可以有多个条目,而 Guid of Employee 将在 Guid Field of Address 中。
同样,一个公司也可以有多个地址。 Guid of Company 将在 Guid of Address 中。

能否请您帮助我如何使用 Fluent API 配置关系 b/w Employee-Address 和 Company-Address

public class Employee
{
public int EmployeeId;
public Guid Guid;
.
.
.
public ICollection<Address> Addresses;
}

public class Company
{
public int CompanyId;
public Guid Guid;
.
.
.
public ICollection<Address> Addresses;
}

public class Address
{
public int AddressId
public Guid Guid; // Guid from Employee or Company
.
.
. // Should here be Navigation to Employee/Company as well?


}

最佳答案

我不确定我是否理解您的问题。你想要像这样的两个简单的 1:N 关系吗?:

Emplyee 1:N Adress
Company 1:N Adress

如果是这种情况,你应该有这个模型:

public class Employee
{
public int EmployeeId { get; set; };
// ...
public virutal ICollection<Address> Addresses { get; set; };
}

public class Company
{
public int CompanyId { get; set; };
// ...
public ICollection<Address> Addresses { get; set; };
}

public class Address
{
public int AddressId { get; set; };
public int? EmployeeId { get; set; };
public int? CompanyId { get; set; };
// ...
public virtual Employee Employee { get; set; };
public virtual Company Company { get; set; };
}

关于c# - Fluent API - 一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40152750/

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