简体   繁体   English

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

[英]nhibernate mapping by code identity not insert

I'm using nhibernate 5,1,1. 我正在使用nhibernate 5,1,1。 Mapping by code when you add an entry, you send 2 requests 添加条目时按代码映射,您发送2个请求

select max (id) From Bred 从品种中选择最大(id)

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

I need the field id not to be sent in the insert request and there was no first request. 我需要在插入请求中不发送字段ID,并且没有第一个请求。 Id auto increment How can I do that? 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... 我需要在插入请求中不发送字段ID,并且没有第一个请求...

In case that our DB is supporting IDENTITY (auto increment on DB side) , we should not use Increment , but Native setting (or Identity ) 如果我们的数据库支持IDENTITY (数据库侧的自动增量) ,则不应使用Increment ,而应使用本Native设置(或Identity

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

Check the doc for detailed explanation 检查文档以获取详细说明

5.1.5.1. 5.1.5.1。 generator 发电机

small extract 小提取物

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. 支持DB2,MySQL,MS SQL Server和Sybase中的标识列。 The identifier returned by the database is converted to the property type using Convert.ChangeType. 使用Convert.ChangeType将数据库返回的标识符转换为属性类型。 Any integral property type is thus supported. 因此,支持任何整数属性类型。

... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM