nhibernate,行被另一个事务更新或删除(或未保存的值映射不正确)

nhibernate, Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

插入新信息时出错。 更新现有信息时,不会出现问题。 在不使用代码的情况下将相同的信息放入数据库时​​也没有问题。 测试代码不会尝试同时存储相同的信息。

映射文件

<class name="FileInfo" table="update_list" dynamic-update="true" >
    <cache usage="read-write"/>
    <id name="Name" type="string">
      <generator class="native" />
    </id>
    <property name="Version" />
    <property name="pkg" />
    <property name="UpdateDate" />
    <property name="Size" />
    <property name="Path" />
    <property name="AutoUpdate" type="TrueFalse" />
  </class>

实体

public class FileInfo
    {
        public virtual string Name { get; set; }
        public virtual string Version { get; set; }
        public virtual string pkg { get; set; }
        public virtual DateTime UpdateDate { get; set; }
        public virtual double Size { get; set; }
        public virtual string Path { get; set; }
        public virtual bool AutoUpdate { get; set; }
    }

节省部分

using (var db = Database.Open())
using (var trans = db.BeginTransaction())
            {
                FileInfo new_file = FileInfoDao.Get(db).GetBypkgName(postedFile.FileName);
                if (new_file == null)
                {
                    new_file = new FileInfo();
                }
                new_file.Name = postedFile.FileName;
                new_file.Size = postedFile.ContentLength;
                new_file.Path = filePath;
                new_file.UpdateDate = DateTime.Now;
                new_file.AutoUpdate = true;
                var info = apkHelper.GetInfo(filePath);
                new_file.pkg = info.Value.Item1;
                new_file.Version = info.Value.Item2;
                try
                {
                    db.SaveOrUpdate(new_file);
                    trans.Commit();
                }
                catch (Exception e)
                {
                    log.Error(e);
                }
}

你的名字就是你的PK,你来定义

<generator class="native" />  

但你设置了 new_file.Name ...尝试使用

<generator class="assigned" />

检查所有选项:https://www.javatpoint.com/generator-classes