【发布时间】:2019-11-22 14:49:24
【问题描述】:
假设我有两个实体,bungalows 和 apartments。它们都有不同的字段并且不能互换,但是这两个实体都有多个租户。每个租户只能是一间平房或一间公寓的一部分。如何使用 Entity Framework 实现这一点?
我正在考虑再创建 2 个实体 bungalowTenants 和 apartmentTenants 并使用它们进行映射。每个bungalowTenant 将有一个bungalow 和tenant 的实例,apartmentTenant 的实例也是如此。
Bungalows 会有 bungalowTenants 和 apartmentTenants 的集合。
public class Bungalow
{
public int Id { get; set; }
public int HouseNumber { get; set; }
public string Street { get; set; }
public ICollection<BungalowTenants> Tenants { get; set; }
}
public class Apartment
{
public int Id { get; set; }
public int ApartmentNumber{ get; set; }
public string Wing{ get; set; }
public string Building{ get; set; }
public ICollection<ApartmentTenants> Tenants { get; set; }
}
public class Tenant
{
public int Id{ get; set; }
public string Name{ get; set; }
}
public class ApartmentTenants
{
public int ApartmentId { get; set; }
public Apartment Apartment{ get; set; }
public int TenantId{ get; set; }
public Tenant Tenant{ get; set; }
}
public class BungalowTenants
{
public int BungalowId{ get; set; }
public Bungalow Bungalow{ get; set; }
public int TenantId{ get; set; }
public Tenant Tenant{ get; set; }
}
这种方法的问题在于,它不会以任何方式限制同一租户同时成为平房和公寓的一部分。我无法弄清楚如何使用实体框架来做到这一点。对于此事,我将不胜感激。
【问题讨论】:
标签: c# asp.net .net entity-framework asp.net-core