Fluent NHibernate - 新版本中的 HasMany 错误
我将 NHibernate 升级为 3.1.0.4000,将 Fluent 升级为 1.2.0.712,并且 HasMany 存在一些问题...
我的实体:
public class MateriaPrima
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
public virtual DateTime Date { get; set; }
public virtual decimal Price { get; set; }
}
public class Product
{
public virtual int Id { get; set; }
public virtual IList<ProductMateriaPrima> ListMateriaPrima { get; set; }
public virtual string Description { get; set; }
public virtual decimal Price { get; set; }
public virtual DateTime Date { get; set; }
public Product()
{
this.ListaMateriaPrima = new List<ProductMateriaPrima>();
}
}
public class ProductMateriaPrima
{
public virtual int Id { get; set; }
public virtual Product Product {get;set;}
public virtual MateriaPrima MateriaPrima { get; set; }
public virtual decimal PrecoCusto {get;set;}
}
和地图:
public class MateriaPrimaMap : ClassMap<MateriaPrima>
{
public MateriaPrimaMap()
{
Id(m => m.Id).Length(11).Not.Nullable();
Map(m => m.Description).Length(90).Not.Nullable();
Map(m => m.Date).Not.Nullable();
Map(m => m.Price).Not.Nullable();
}
}
public class ProductMateriaPrimaMap : ClassMap<ProductoMateriaPrima>
{
public ProductMateriaPrimaMap()
{
Id(c => c.Id).Length(11);
Map(c => c.Price).Not.Nullable();
References(c => c.MateriaPrima).Column("IdMateriaPrima").Not.LazyLoad();
References(c => c.Product).Column("IdProduct").Not.LazyLoad();
}
}
public class ProdutoMap : ClassMap<Produto>
{
public ProdutoMap()
{
Id(m => m.Id).Length(11).Not.Nullable();
Map(m => m.Description).Length(90).Not.Nullable();
Map(m => m.Price).Length(10);
Map(m => m.Date).Length(12);
Map(m => m.Active).Not.Nullable();
HasMany(x => x.ListaMateriaPrima)
.Table("ProdutoMateriaPrima")
.KeyColumn("IdProduto")
.KeyColumn("IdMateriaPrima")
.Inverse()
.Cascade.AllDeleteOrphan();
}
}
当我尝试搜索时,我收到错误:{“未知列 'listamater0_.MateriaPrima_id' in 'field list'"}
当我使用旧版本的 Nhibernate 和 Fluent 时,不会发生此错误... 有人知道发生了什么事吗? 感谢您的帮助...
I Upgrade my NHibernate for 3.1.0.4000 and Fluent for 1.2.0.712 and have some problems with HasMany...
my entities:
public class MateriaPrima
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
public virtual DateTime Date { get; set; }
public virtual decimal Price { get; set; }
}
public class Product
{
public virtual int Id { get; set; }
public virtual IList<ProductMateriaPrima> ListMateriaPrima { get; set; }
public virtual string Description { get; set; }
public virtual decimal Price { get; set; }
public virtual DateTime Date { get; set; }
public Product()
{
this.ListaMateriaPrima = new List<ProductMateriaPrima>();
}
}
public class ProductMateriaPrima
{
public virtual int Id { get; set; }
public virtual Product Product {get;set;}
public virtual MateriaPrima MateriaPrima { get; set; }
public virtual decimal PrecoCusto {get;set;}
}
And Maps:
public class MateriaPrimaMap : ClassMap<MateriaPrima>
{
public MateriaPrimaMap()
{
Id(m => m.Id).Length(11).Not.Nullable();
Map(m => m.Description).Length(90).Not.Nullable();
Map(m => m.Date).Not.Nullable();
Map(m => m.Price).Not.Nullable();
}
}
public class ProductMateriaPrimaMap : ClassMap<ProductoMateriaPrima>
{
public ProductMateriaPrimaMap()
{
Id(c => c.Id).Length(11);
Map(c => c.Price).Not.Nullable();
References(c => c.MateriaPrima).Column("IdMateriaPrima").Not.LazyLoad();
References(c => c.Product).Column("IdProduct").Not.LazyLoad();
}
}
public class ProdutoMap : ClassMap<Produto>
{
public ProdutoMap()
{
Id(m => m.Id).Length(11).Not.Nullable();
Map(m => m.Description).Length(90).Not.Nullable();
Map(m => m.Price).Length(10);
Map(m => m.Date).Length(12);
Map(m => m.Active).Not.Nullable();
HasMany(x => x.ListaMateriaPrima)
.Table("ProdutoMateriaPrima")
.KeyColumn("IdProduto")
.KeyColumn("IdMateriaPrima")
.Inverse()
.Cascade.AllDeleteOrphan();
}
}
When i try to search, i got the error: {"Unknown column 'listamater0_.MateriaPrima_id' in 'field list'"}
This error don´t happen when i had the old version of Nhibernate and Fluent...
Someone know what´s happening?
Thanks for the help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生此错误是因为您没有设置 Id 属性的列名称,id 的默认模式是:entityname_id。
现在在 ProductMateriaPrimaMap 类中,您写道:
这意味着在您的 MateriaPrimaMap 类中,您应该有列名称为 IdMateriaPrima 的 Id,但默认名称为:MateriaPrima_Id。
我无法确切地说问题出在哪里,因为您没有显示查询。但我知道当 Id 列名称与参考列名称不同时会发生此错误。
This error happend because you didn't set collumn name of Id property, default pattern for id is: entityname_id.
And now in ProductMateriaPrimaMap class you wrote:
This mean that in you MateriaPrimaMap class you should have Id with collumn name: IdMateriaPrima, but default you have name: MateriaPrima_Id.
I can't say where the problem is exactly because you didn't show the query. But i know that this error happend when Id column name is diffrent from Reference column name.