public class Product : EntityBase<Product, int>, IAggregateRoot
{
public virtual string ProductName { get; set; }
public virtual List<ProductDetail> Description { get; set; }
}
public class ProductDetail : EntityBase<ProductDetail, int>, IAggregateRoot
{
public virtual string Description { get; set; }
public virtual Product Product { get; set; }
}
上述产品实体有多个ProductDetails。我的映射如下:
public class ProductMap : ClassMapping<Product>
{
public ProductMap()
{
Lazy(false);
Table("Product");
Id(x => x.ID, map => { map.Column("ID"); map.Generator(Generators.Native); });
Property(x => x.ProductName, map => map.NotNullable(true));
Bag(x => x.Description, m => {
m.Inverse(true); // Is collection inverse?
m.Cascade(Cascade.All); //set cascade strategy
m.Key(k => k.Column(col => col.Name("ProductID"))); //foreign key in Detail table
}, a => a.OneToMany());
}
}
public class ProductDetailMap : ClassMapping<ProductDetail>
{
public ProductDetailMap()
{
Lazy(false);
Table("ProductDetail");
Id(x => x.ID, map => { map.Column("ID"); map.Generator(Generators.Native); });
Property(x => x.Description, map => map.NotNullable(false));
ManyToOne(x => x.Product, x =>
{
x.Column("ProductID");
});
}
}
当我保存这个;我得到了错误。
NHibernate.dll中发生了'NHibernate.PropertyAccessException'类型的异常,但未在用户代码中处理 附加信息:无效的转换(检查您的映射是否存在属性类型不匹配);
答案 0 :(得分:1)
对于地图集合,我们必须使用接口( IList<>
)
public class Product : EntityBase<Product, int>, IAggregateRoot
{
public virtual string ProductName { get; set; }
//public virtual List<ProductDetail> Description { get; set; }
public virtual IList<ProductDetail> Description { get; set; }
}
NHibernate会注入自己的IList<>
实现 - 这不是List
的孩子......这是代理所需要的。 ..懒加载