[英]FluentNHibernate mapping of composite foreign keys
我有一个现有的数据库架构,希望用Fluent.NHibernate替换自定义数据访问代码。 由于数据库模式已经存在于运输产品中,因此无法更改。 并且优选的是,域对象不改变或仅改变很小。
我在映射用以下表结构说明的一种异常模式结构时遇到了麻烦:
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的集合的方法。 此外,Container类具有一个方法,该方法返回对象图中所有Property对象的集合。 我最好的猜测是,这是一种便捷方法,还是一种从未删除过的传统方法。
业务逻辑主要与Item(作为聚合根)一起使用,并且仅在添加或删除Items时与Container一起使用。
我尝试了几种技术来映射它,但是没有用,所以除非有人要求,否则我不会在这里包括它们。 您将如何映射?
映射应如下所示:
public sealed class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table("Categories");
CompositeId()
.KeyProperty(c => c.ItemId, "ItemId")
.KeyProperty(c => c.CategoryId, "CategoryId");
}
}
注意:
--
public class Category
{
public virtual int ItemId { get; set; }
public virtual int CategoryId { get; set; }
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Category)) return false;
return Equals((Category) obj);
}
public virtual bool Equals(Category other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.ItemId == ItemId && other.CategoryId == CategoryId;
}
public override int GetHashCode()
{
unchecked
{
return (ItemId*397) ^ CategoryId;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.