C# Fluent API-一对多关系

C# Fluent API-一对多关系,c#,entity-framework,ef-fluent-api,C#,Entity Framework,Ef Fluent Api,我有两个实体,即员工和公司。两者都可以有一个或多个地址由于Guid始终是唯一的,所以我希望将员工和公司中的Guid用作地址中的外键。 也就是说,员工的地址中可以有多个条目,员工的Guid将位于地址的Guid字段中。 同样,一家公司也可以有多个地址。公司的Guid将位于地址的Guid中 您能帮助我如何使用Fluent API配置带员工地址和公司地址的关系吗 public class Employee { public int EmployeeId; public Guid Guid

我有两个实体,即员工和公司。两者都可以有一个或多个地址由于Guid始终是唯一的,所以我希望将员工和公司中的Guid用作地址中的外键。
也就是说,员工的地址中可以有多个条目,员工的Guid将位于地址的Guid字段中。
同样,一家公司也可以有多个地址。公司的Guid将位于地址的Guid中

您能帮助我如何使用Fluent API配置带员工地址和公司地址的关系吗

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?


}
公共类员工
{
公共国际雇员ID;
公共Guid;
.
.
.
公共i收集地址;
}
公营公司
{
公共国际公司;
公共Guid;
.
.
.
公共i收集地址;
}
公共课堂演讲
{
公共整数地址ID
公共Guid;//来自员工或公司的Guid
.
.
.//这里也应该是员工/公司的导航吗?
}

我不确定是否理解您的问题。你想要两个简单的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; };
}
公共类员工
{
public int EmployeeId{get;set;};
// ...
公共虚拟ICollection地址{get;set;};
}
公营公司
{
public int CompanyId{get;set;};
// ...
公共ICollection地址{get;set;};
}
公共课堂演讲
{
公共int地址ID{get;set;};
public int?EmployeeId{get;set;};
公共int?CompanyId{get;set;};
// ...
公共虚拟员工{get;set;};
公共虚拟公司{get;set;};
}

设置您喜欢的实体

public class Employee
{
    //no need of following line. just use the GUID as Employee id
    //public int EmployeeId;  
    public Guid EmployeeId;
    .
    .
    .
    public ICollection<Address> Addresses;
}

public class Company
{
    public int CompanyId;//no need of this line, use guid as company id
    public Guid CompanyId;
    .
    .
    .
    public ICollection<Address> Addresses;
}

public class Address
{
    public int AddressId
    public Guid OwnerId; // Guid from Employee or Company
    .
    .
    //don't add Navigation to Employee/Company


}
公共类员工
{
//不需要以下行。只需使用GUID作为员工id
//公共国际雇员ID;
公共Guid EmployeeId;
.
.
.
公开的ICollection建议

modelBuilder.Entity()
.HasMany(c=>c.address)
.WithRequired()
.HasForeignKey(a=>a.OwnerId);
modelBuilder.Entity()
.HasMany(c=>c.address)
.WithRequired()
.HasForeignKey(a=>a.OwnerId);
modelBuilder.Entity<Company>()
.HasMany(c => c.Addresses)
.WithRequired()
.HasForeignKey(a => a.OwnerId);

modelBuilder.Entity<Employee>()
.HasMany(c => c.Addresses)
.WithRequired()
.HasForeignKey(a => a.OwnerId);