在多租户代码优先 Entity Framework 中引用 "partition table" 外键
Referencing a "partition table" foreign key in Multi-Tenant Code-First Entity Framework
我正在创建一个需要在单个数据库中构建的多租户应用程序。为了对表进行分区,我们有一个 Tenant
实体,其主键将被引用为需要分区的其他表的键的一部分。 Tenant
实体如下所示:
public class Tenant
{
[Key]
public string TenantId { get; set; }
public string TenantName { get; set; }
}
使用此分区的示例是在 Store
-Item
场景中,其中租户可以拥有多个商店以及多个项目。商店可以有多个项目,它们的关系在 StoreItem
实体中维护。我们当前的实现如下所示:
商店实体
public class Store
{
[Key, Column(Order = 1)]
public string TenantId { get; set; }
[Key, Column(Order = 2)]
public string StoreId { get; set; }
[ForeignKey("TenantId")]
public virtual Tenant Tenant { get; set; }
}
项目实体
public class Item
{
[Key, Column(Order = 1)]
public string TenantId { get; set; }
[Key, Column(Order = 2)]
public string ItemId { get; set; }
[ForeignKey("TenantId")]
public virtual Tenant Tenant { get; set; }
}
StoreItem 实体
public class StoreItem
{
[Key, Column(Order = 1)]
public string TenantId { get; set; }
[Key, Column(Order = 2)]
public string StoreId { get; set; }
[Key, Column(Order = 3)]
public string ItemId { get; set; }
[ForeignKey("TenantId")]
public virtual Tenant Tenant { get; set; }
[ForeignKey("StoreId")]
public virtual Store Store { get; set; }
[ForeignKey("ItemId")]
public virtual Item Item { get; set; }
}
当我们尝试建立数据库时,我遇到了 ff。错误:
StoreItem_Item_Target_StoreItem_Item_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
StoreItem_Store_Target_StoreItem_Store_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
我构建密钥的方式有什么问题? TenantId
不应该被引用为其他实体的密钥的一部分吗?
问题是您使用 [ForeignKey("ItemId")]
来描述 StoreItem
和 Item
之间的部分关系,但 Item
有多个键列。如果您需要 TenantId
成为密钥的一部分——如果 ItemId
足以唯一标识一条 Item
记录,则您不应该这样做——那么我认为您需要使用流利的API来定义关系。
另一方面,我猜您不需要 TenantId
作为 Item
或 Store
的键列,这会大大简化事情。
我正在创建一个需要在单个数据库中构建的多租户应用程序。为了对表进行分区,我们有一个 Tenant
实体,其主键将被引用为需要分区的其他表的键的一部分。 Tenant
实体如下所示:
public class Tenant
{
[Key]
public string TenantId { get; set; }
public string TenantName { get; set; }
}
使用此分区的示例是在 Store
-Item
场景中,其中租户可以拥有多个商店以及多个项目。商店可以有多个项目,它们的关系在 StoreItem
实体中维护。我们当前的实现如下所示:
商店实体
public class Store
{
[Key, Column(Order = 1)]
public string TenantId { get; set; }
[Key, Column(Order = 2)]
public string StoreId { get; set; }
[ForeignKey("TenantId")]
public virtual Tenant Tenant { get; set; }
}
项目实体
public class Item
{
[Key, Column(Order = 1)]
public string TenantId { get; set; }
[Key, Column(Order = 2)]
public string ItemId { get; set; }
[ForeignKey("TenantId")]
public virtual Tenant Tenant { get; set; }
}
StoreItem 实体
public class StoreItem
{
[Key, Column(Order = 1)]
public string TenantId { get; set; }
[Key, Column(Order = 2)]
public string StoreId { get; set; }
[Key, Column(Order = 3)]
public string ItemId { get; set; }
[ForeignKey("TenantId")]
public virtual Tenant Tenant { get; set; }
[ForeignKey("StoreId")]
public virtual Store Store { get; set; }
[ForeignKey("ItemId")]
public virtual Item Item { get; set; }
}
当我们尝试建立数据库时,我遇到了 ff。错误:
StoreItem_Item_Target_StoreItem_Item_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
StoreItem_Store_Target_StoreItem_Store_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
我构建密钥的方式有什么问题? TenantId
不应该被引用为其他实体的密钥的一部分吗?
问题是您使用 [ForeignKey("ItemId")]
来描述 StoreItem
和 Item
之间的部分关系,但 Item
有多个键列。如果您需要 TenantId
成为密钥的一部分——如果 ItemId
足以唯一标识一条 Item
记录,则您不应该这样做——那么我认为您需要使用流利的API来定义关系。
另一方面,我猜您不需要 TenantId
作为 Item
或 Store
的键列,这会大大简化事情。