NHibernate 一对多映射和保存
NHibernate One to Many Mapping and Saving
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; }
}
上述产品实体有多个产品详细信息。我的映射如下;
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");
});
}
}
当我保存这个时;我遇到错误。
An exception of type 'NHibernate.PropertyAccessException' occurred in NHibernate.dll but was not handled in user code
Additional information: Invalid Cast (check your mapping for property type mismatches);
对于映射集合,我们必须使用接口(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
的子级...这是 [=18= 所需要的]代理...延迟加载
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; }
}
上述产品实体有多个产品详细信息。我的映射如下;
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");
});
}
}
当我保存这个时;我遇到错误。
An exception of type 'NHibernate.PropertyAccessException' occurred in NHibernate.dll but was not handled in user code Additional information: Invalid Cast (check your mapping for property type mismatches);
对于映射集合,我们必须使用接口(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
的子级...这是 [=18= 所需要的]代理...延迟加载