映射自引用IDictionary<;字符串、实体>;用流利的NHibernate
我的域模型中有以下实体:映射自引用IDictionary<;字符串、实体>;用流利的NHibernate,nhibernate,dictionary,fluent-nhibernate,mapping,Nhibernate,Dictionary,Fluent Nhibernate,Mapping,我的域模型中有以下实体: class Entity { public int Id { get; set; } } class Foo : Entity { public IDictionary<string, Attribute> Attributes { get; set; } } class Bar : Entity { public IDictionary<string, Attribute> Attributes { get; set
class Entity
{
public int Id { get; set; }
}
class Foo : Entity
{
public IDictionary<string, Attribute> Attributes { get; set; }
}
class Bar : Entity
{
public IDictionary<string, Attribute> Attributes { get; set; }
}
class Attribute : Entity
{
public string Key { get; set; }
public string Value { get; set; }
public IDictionary<string, Attribute> Attributes { get; set; }
}
如果我将If
语句替换为以下内容:
if (entityType == typeof(Attribute))
{
manyToMany
.Table("Attribute")
.ParentKeyColumn("ParentAttributeId");
}
我得到以下例外情况:
NHibernate.FKUnmatchingColumnsException:外键(FK_属性_属性[ParentAttributeId])必须与引用的主键(属性[ParentAttributeId,key])具有相同的列数
这是由于NHibernate自动将键
设置为my属性
列中Id
旁边的主键。我希望Key
不是主键,因为它显示在我所有的多对多表中
create table FooAttribute (
FooId INT not null,
AttributeId INT not null,
[Key] NVARCHAR(255) not null
)
我希望外键只引用Id
,而不是(Id,Key)
,因为将Key
作为主键需要它是唯一的,这不会在我所有的manytomy
s中出现。- 您在哪里映射属性本身(它是否包含复合键)
可能是一个更好的名称,以表明它包含一个值李>AttributeValue
足以说明Key应该是字典键.AsMap(x=>x.Key)
create table FooAttribute ( FooId INT not null, AttributeValueId INT not null )
或考虑使用
mapping.HasMany(x => x.Attributes)
.KeyColumn(entity + Id)
.AsMap(x => x.Key)
.Cascade.AllDeleteOrphan();
这将创造
create table Attribute (
Id INT not null,
FooId INT,
BarId INT,
ParentAttributeId INT,
Key TEXT,
Value TEXT,
)
- 您在哪里映射属性本身(它是否包含复合键)
可能是一个更好的名称,以表明它包含一个值李>AttributeValue
足以说明Key应该是字典键.AsMap(x=>x.Key)
create table FooAttribute ( FooId INT not null, AttributeValueId INT not null )
或考虑使用
mapping.HasMany(x => x.Attributes)
.KeyColumn(entity + Id)
.AsMap(x => x.Key)
.Cascade.AllDeleteOrphan();
这将创造
create table Attribute (
Id INT not null,
FooId INT,
BarId INT,
ParentAttributeId INT,
Key TEXT,
Value TEXT,
)