映射自引用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
    可能是一个更好的名称,以表明它包含一个值
  • .AsMap(x=>x.Key)
    足以说明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
    可能是一个更好的名称,以表明它包含一个值
  • .AsMap(x=>x.Key)
    足以说明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,
    )