Nhibernate 主键必须是唯一的
我已经在 C# 中使用本地 SQLite 数据库设置了一个 Nhibernate 项目,如果在保存我的 Nhibernate 对象时遇到问题,它会告诉我我的主键不是唯一的,没错,但我在我的 nhibernate 映射中添加了一个生成器。
这是类:
public class Article
{
public virtual int ArticleId { get; set; }
public virtual string Name { get; set; }
public virtual double Price { get; set; }
public virtual string ArticleNumber { get; set; }
public virtual string Description { get; set; }
public virtual Sales Sales {get; set;}
}
这是映射文件:
<class name="Article">
<id name="ArticleId">
<generator class="identity" />
</id>
<property name="Name" />
<property name="Price" />
<property name="ArticleNumber" />
<property name="Description" />
<many-to-one name="Sales" class="Sales" column="SalesId"/>
</class>
我正在创建一些文章,并将其添加到销售中,如果我想保存我的销售,它会出现此 PrimaryKey 异常。
是否存在已知问题?
非常感谢,Alex
销售地图:
<class name="Sales">
<id name="SaleId" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Amount" />
<property name="Price" />
<bag name="OrderList" cascade="all">
<key column="ArticleId"/>
<one-to-many class="Article"/>
</bag>
</class>
public class Sales
{
public Sales()
{
if (this.orderList == null)
orderList = new List<Article>();
}
private IList<Article> orderList;
public virtual int SaleId { get; set; }
public virtual double Price { get; set; }
public virtual int Amount { get; set; }
public virtual IList<Article> OrderList
{
get
{
return this.orderList;
}
set
{
this.orderList = value;
}
}
}
I've set up a Nhibernate Project in C# with a Local SQLite Database, and if got an issue while saving my Nhibernate object, it says me that my primarykey is not unique, thats right but i added a Generator to my nhibernate-mapping.
Here is the Class:
public class Article
{
public virtual int ArticleId { get; set; }
public virtual string Name { get; set; }
public virtual double Price { get; set; }
public virtual string ArticleNumber { get; set; }
public virtual string Description { get; set; }
public virtual Sales Sales {get; set;}
}
And here is the Mapping File:
<class name="Article">
<id name="ArticleId">
<generator class="identity" />
</id>
<property name="Name" />
<property name="Price" />
<property name="ArticleNumber" />
<property name="Description" />
<many-to-one name="Sales" class="Sales" column="SalesId"/>
</class>
Im Creating Some Articles, and im adding it to Sales, if i want to save my Sales, it got this PrimaryKey exception.
Is there a known issue?
Very Thanks, Alex
Sales Mapping:
<class name="Sales">
<id name="SaleId" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Amount" />
<property name="Price" />
<bag name="OrderList" cascade="all">
<key column="ArticleId"/>
<one-to-many class="Article"/>
</bag>
</class>
public class Sales
{
public Sales()
{
if (this.orderList == null)
orderList = new List<Article>();
}
private IList<Article> orderList;
public virtual int SaleId { get; set; }
public virtual double Price { get; set; }
public virtual int Amount { get; set; }
public virtual IList<Article> OrderList
{
get
{
return this.orderList;
}
set
{
this.orderList = value;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您这样设置身份映射:
确保 PK 字段是身份字段。
然后在销售映射中:
对于销售映射也是如此。确保其 id 映射的未保存值为 0,并且 PK 被标记为身份字段。
I suggest setting up your identity mapping as such:
Ensure the PK field is an identity field.
Then in your mapping for Sales:
Same thing for your Sales mapping. Ensure it's id mapping has an unsaved value of 0 and the PK is marked as an identity field.
问题是我正在使用事务,没有事务它工作得很好。
感谢杰弗里·约胡姆的帮助。
the problem was that i was using a Transaction, without Transaction it worked very well.
Thanks Jeffrey Jochum for your help.