Nhibernate更新问题-参数索引超出范围
我在将数据更新到数据库时遇到问题。我有一个简单的多对一产品-组关系:
public class GroupInfoMapping : ClassMap<GroupInfo>
{
public GroupInfoMapping()
{
Table("`group`");
Schema("tempdb");
Id(x => x.Id)
.GeneratedBy.Native();
OptimisticLock.Version();
Map(x => x.GroupName);
Map(x => x.ParentId);
HasMany(x => x.Products)
.KeyColumn("GroupId")
.Inverse()
.Fetch.Select()
.AsSet();
//HasMany(x => x.Children)
// .KeyColumn("Id")
// .Inverse()
// .Fetch.Select()
// .Cascade.None()
// .AsSet();
//References(x => x.Parent)
// .Class(typeof(GroupInfo))
// .Column("ParentId")
// .Fetch.Select();
}
}
public class ProductInfoMapping : ClassMap<ProductInfo>
{
public ProductInfoMapping()
{
Table("`product`");
Schema("tempdb");
Id(x => x.Id)
.GeneratedBy.Native();
OptimisticLock.Version();
Map(x => x.GroupId);
Map(x => x.ProductName);
References(x => x.Group)
.Class(typeof(GroupInfo))
.Column("GroupId")
.Fetch.Select();
}
}
如果我仅更改产品数据,则没有区别,它不允许我更新异常: 参数索引超出范围。
如果我将 Not.Update() 添加到产品映射以供组引用,我就可以更新产品数据(但组仍然未被引用)。
我正在使用 MySql 数据库,并且每个演示者使用一个会话。为什么我无法同时更新实体及其引用?我已经注释掉了自引用组的部分,因为这在仅更新组时也会产生问题。
谢谢, 戈兰
I am having problem with updating data to database. I have a simple many-to-one Product - Group relationship:
public class GroupInfoMapping : ClassMap<GroupInfo>
{
public GroupInfoMapping()
{
Table("`group`");
Schema("tempdb");
Id(x => x.Id)
.GeneratedBy.Native();
OptimisticLock.Version();
Map(x => x.GroupName);
Map(x => x.ParentId);
HasMany(x => x.Products)
.KeyColumn("GroupId")
.Inverse()
.Fetch.Select()
.AsSet();
//HasMany(x => x.Children)
// .KeyColumn("Id")
// .Inverse()
// .Fetch.Select()
// .Cascade.None()
// .AsSet();
//References(x => x.Parent)
// .Class(typeof(GroupInfo))
// .Column("ParentId")
// .Fetch.Select();
}
}
public class ProductInfoMapping : ClassMap<ProductInfo>
{
public ProductInfoMapping()
{
Table("`product`");
Schema("tempdb");
Id(x => x.Id)
.GeneratedBy.Native();
OptimisticLock.Version();
Map(x => x.GroupId);
Map(x => x.ProductName);
References(x => x.Group)
.Class(typeof(GroupInfo))
.Column("GroupId")
.Fetch.Select();
}
}
There is no difference if I change only the product data, it will not allow me to update with exception:
Parameter index is out of range.
If I add Not.Update() to Product Mapping for Group reference, I am able to update product data (but Group remains unreferenced).
I am using MySql database and I am using one session per presenter. Why I cannot update both entity and its reference? I have commented out part for selfreferening the group, since this also creates problem when updating group only.
Thanks,
Goran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的 ProductInfoMapping 类中,您将同一列映射两次:
并且
不需要组 id 的 Map,因为您可以在没有它的情况下访问 Group 属性的 id:
更新:
如果您仍然希望有一个 GroupId 属性,您可以访问该组的 id,而不是直接在 Group 对象上访问它,您可以像这样修改该属性(示例是id 与 Guid 类型):
In your ProductInfoMapping class, you are mapping the same column twice:
and
The Map of the group id is unneeded as you can access the id of the Group property without it:
Update:
If you still desire that there be a GroupId property which you can access the id of the Group instead of accessing it directly on the Group object, you can just modify the property like so (example is for an id with a Guid type):