C# 复合外键的FluentNHibernate映射
我有一个现有的数据库模式,希望用Fluent.NHibernate替换自定义数据访问代码。无法更改数据库架构,因为它已存在于配送产品中。如果域对象没有改变或者只改变了最小的值,则更可取 我在映射一个不寻常的模式结构时遇到了问题,该结构如以下表格结构所示:C# 复合外键的FluentNHibernate映射,c#,nhibernate,fluent-nhibernate,nhibernate-mapping,C#,Nhibernate,Fluent Nhibernate,Nhibernate Mapping,我有一个现有的数据库模式,希望用Fluent.NHibernate替换自定义数据访问代码。无法更改数据库架构,因为它已存在于配送产品中。如果域对象没有改变或者只改变了最小的值,则更可取 我在映射一个不寻常的模式结构时遇到了问题,该结构如以下表格结构所示: CREATE TABLE [Container] ( [ContainerId] [uniqueidentifier] NOT NULL, CONSTRAINT [PK_Container] PRIMARY KEY ( [Con
CREATE TABLE [Container] (
[ContainerId] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_Container] PRIMARY KEY (
[ContainerId] ASC
)
)
CREATE TABLE [Item] (
[ItemId] [uniqueidentifier] NOT NULL,
[ContainerId] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_Item] PRIMARY KEY (
[ContainerId] ASC,
[ItemId] ASC
)
)
CREATE TABLE [Property] (
[ContainerId] [uniqueidentifier] NOT NULL,
[PropertyId] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_Property] PRIMARY KEY (
[ContainerId] ASC,
[PropertyId] ASC
)
)
CREATE TABLE [Item_Property] (
[ContainerId] [uniqueidentifier] NOT NULL,
[ItemId] [uniqueidentifier] NOT NULL,
[PropertyId] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_Item_Property] PRIMARY KEY (
[ContainerId] ASC,
[ItemId] ASC,
[PropertyId] ASC
)
)
CREATE TABLE [Container_Property] (
[ContainerId] [uniqueidentifier] NOT NULL,
[PropertyId] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_Container_Property] PRIMARY KEY (
[ContainerId] ASC,
[PropertyId] ASC
)
)
现有域模型具有以下类结构:
Property类包含表示属性名称和值的其他成员。ContainerProperty和ItemProperty类没有其他成员。它们的存在只是为了确定财产的所有者。Container和Item类具有分别返回ContainerProperty和ItemProperty集合的方法。此外,容器类还有一个方法,用于返回对象图中所有属性对象的集合。我最好的猜测是,这要么是一个方便的方法,要么是一个从未被删除的遗留方法
业务逻辑主要用于项(作为聚合根),并且仅在添加或删除项时用于容器
我已经尝试了几种映射技术,但都不起作用,所以除非有人要求,否则我不会在这里包含它们。您将如何对此进行映射?映射应如下所示:
public sealed class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table("Categories");
CompositeId()
.KeyProperty(c => c.ItemId, "ItemId")
.KeyProperty(c => c.CategoryId, "CategoryId");
}
}
映射应该这样看:
public sealed class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table("Categories");
CompositeId()
.KeyProperty(c => c.ItemId, "ItemId")
.KeyProperty(c => c.CategoryId, "CategoryId");
}
}