通过代码标识的 nhibernate 映射不插入

nhibernate mapping by code identity not insert

我正在使用 nhibernate 5,1,1。按代码映射 添加条目时,您发送 2 个请求

select 最大 (id) 来自 Bred

insert Into Bred (Id, Name, PetType) valuses ({value of max (id)}, text, 1)

我需要在插入请求中不发送字段 ID,并且没有第一个请求。 id自增 我怎样才能做到这一点?

public abstract class BaseEntity
{
    /// <summary>
    /// Ин.
    /// </summary>
    public virtual int Id { get; set; }

    /// <summary>
    /// Дата добавления
    /// </summary>
    public virtual DateTime DateInsert { get; set; }
}    

public abstract class BaseMapping<T> : ClassMapping<T> where T : BaseEntity
{
    protected BaseMapping(string nameTabel)
    {
        this.Table(nameTabel);
        this.Id(x => x.Id, map =>
        {
            map.Generator(Generators.Increment);
            map.Column("\"Id\"");

        });
        this.Property(x => x.DateInsert, x =>
        {
            x.Column("\"DateInsert\"");
            x.Insert(false);
            x.Update(false);

        });
    }
}

/// <summary>
/// Справочник пород
/// </summary>
public class Breed : BaseEntity
{
    /// <summary>
    /// Название
    /// </summary>
    public virtual string Name { get; set; }

    /// <summary>
    /// Тип животных 
    /// </summary>
    public virtual PetType PetType { get; set; }
}

public class BreedMap : BaseMapping<Breed>
{
    public BreedMap() : base("\"Breed\"")
    {
        this.Property(x => x.Name, x => x.Column("\"Name\""));
        this.Property(x => x.PetType, x => x.Column("\"PetType\""));
    }
}

I need the field id not to be sent in the insert request and there was no first request...

如果我们的数据库支持 IDENTITY (在数据库端自动递增),我们不应该使用 Increment,而是 Native设置(或Identity)

//map.Generator(Generators.Increment);
map.Generator(Generators.Native);

查看文档以获取详细说明

5.1.5.1. generator

小摘

increment

generates identifiers of any integral type that are unique only when no other process is inserting data into the same table. Do not use in a cluster.

...

native/identity

supports identity columns in DB2, MySQL, MS SQL Server and Sybase. The identifier returned by the database is converted to the property type using Convert.ChangeType. Any integral property type is thus supported.

...