流畅的NHibernate有很多参考复合键

时间:2013-02-06 17:54:12

标签: .net fluent-nhibernate

我在这个问题上浪费了很长时间。所以问题是Fluent在创建DB shema时会生成错误:外键(FK6FF7FBACF647ABE5:CheckListToAttributeLinkItem [ProductUID]))必须与引用的主键具有相同的列数(ProductAttributeLink [ProductUID,AttributeEntityUID])。
这是我的实体。我正在那里展示它们(没有Equals(object obj)和GetHashCode()方法) 第一:该实体将Product与其Parameter(Attribute)连接起来。所以它是一个多对多的“中间”表。:

public class ProductAttributeLink
{
    public virtual Product Product { get; set; }
    public virtual AttributeEntity AttributeEntity { get; set; }
}

第二:这个实体是继承者

public class CheckListProductAttributeLinkValue : ProductAttributeLink
{
    protected IList<CheckListToAttributeLinkItem> _values = new List<CheckListToAttributeLinkItem>();
    public virtual IEnumerable<CheckListToAttributeLinkItem> Values
    {
        get { return _values; }
    }
}

第三:这个实体是属性项

public class AttributeEntityValueListItem
{
    public virtual Guid UID { get; set; }
    public virtual string Item { get; set; }
    public virtual AttributeEntity AttributeEntity { get; set; }
}

我需要与此实体和之前的实体(CheckListProductAttributeLinkValue和AttributeEntityValueListItem)进行多对多操作。所以我创建了“中间”实体

public class CheckListToAttributeLinkItem
{
    public virtual CheckListProductAttributeLinkValue CheckListProductAttributeLinkValue { get; set; }
    public virtual AttributeEntityValueListItem AttributeEntityValueListItem { get; set; }
}

映射:

public class ProductAttributeLinkMap : ClassMap<ProductAttributeLink>
{
    public ProductAttributeLinkMap()
    { 
        CompositeId()
            .KeyReference(x => x.Product, "ProductUID")
            .KeyReference(x => x.AttributeEntity, "AttributeEntityUID");

        DiscriminateSubClassesOnColumn("ClassType").Not.Nullable();
    }
}

public class CheckListProductAttributeLinkValueMap :   SubclassMap<CheckListProductAttributeLinkValue>
{
    public CheckListProductAttributeLinkValueMap()
    {
        DiscriminatorValue(@"CheckLiskValue");
        HasMany(x => x.Values)
            .Access.CamelCaseField(Prefix.Underscore)
            .KeyColumns.Add("ProductUID")
            .KeyColumns.Add("AttributeEntityUID")
            .Inverse()
            .Cascade.All();
    }
}

public class CheckListToAttributeLinkItemMap : ClassMap<CheckListToAttributeLinkItem>
{
    public CheckListToAttributeLinkItemMap()
    {
        CompositeId()
            .KeyReference(x => x.CheckLinkProductAttributeLinkValue, "ProductUID", "AttributeEntityUID")
            .KeyReference(x => x.AttributeEntityValueListItem, "AttributeEntityValueListItemUID");
    }
}
public class AttributeEntityValueListItemMap : ClassMap<AttributeEntityValueListItem>
{
    public AttributeEntityValueListItemMap()
    {
        Id(x => x.UID).GeneratedBy.Guid();
        Map(x => x.Item);
        References(x => x.AttributeEntity);
    }
}

0 个答案:

没有答案