Nhibernate 主键必须是唯一的

发布于 2024-10-07 18:15:00 字数 2047 浏览 6 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-10-14 18:15:00

我建议您这样设置身份映射:

<id name="ArticleId" unsaved-value="0">
  <generator class="native" />
</id>

确保 PK 字段是身份字段。

然后在销售映射中:

<many-to-one name="Sales" class="Sales" column="SalesId" insert="true" update="true"/>

对于销售映射也是如此。确保其 id 映射的未保存值为 0,并且 PK 被标记为身份字段。

I suggest setting up your identity mapping as such:

<id name="ArticleId" unsaved-value="0">
  <generator class="native" />
</id>

Ensure the PK field is an identity field.

Then in your mapping for Sales:

<many-to-one name="Sales" class="Sales" column="SalesId" insert="true" update="true"/>

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.

猫弦 2024-10-14 18:15:00

问题是我正在使用事务,没有事务它工作得很好。

感谢杰弗里·约胡姆的帮助。

the problem was that i was using a Transaction, without Transaction it worked very well.

Thanks Jeffrey Jochum for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文