我在这个问题上浪了很久。所以问题是 Fluent 在创建 DB shema 时生成错误:外键 (FK6FF7FBACF647ABE5:CheckListToAttributeLinkItem [ProductUID])) 的列数必须与引用的主键 (ProductAttributeLink [ProductUID, AttributeEntityUID]) 的列数相同。
这是我的实体。我按原样显示在那里(没有简称 Equals(object obj) 和 GetHashCode() 方法)首先:该实体将 Product 与其参数(属性)连接起来。所以这是一个多对多的“中间”表:
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);
}
}