插入一条记录使用NHibernate的复合键 [英] Inserting a record with a Composite Key using NHibernate

查看:177
本文介绍了插入一条记录使用NHibernate的复合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作与使用组合键遗留数据库。我想使用NHibernate的插入一个新的记录到数据库中。 NHibernate的规定,我必须手动创建标识,但是当我尝试插入这个ID,我得到的消息:

  System.Data.SqlClient.SqlException:在无法为表'表名'标识列插入显式值时IDENTITY_INSERT设置为OFF。
 

我不能碰任何的数据库设置,因为它们是由总部设在美国的管理。

我发现,我可以通过做一个数据库插入:

 插入到表名(tablename_country_id,/ *附加字段这里* /)值(16 / *这里多余的值* /)
 

tablename_id 列自动递增。

是否有可能写一些排序的处理,让我来创建一个的ID 与对象的 CountryId 设置并让它自动递增的编号属性。

干杯。


例如code:

表定义:

  CREATE TABLE [DBO]。[表名](
    [tablename_country_id] [INT] NOT NULL,
    [tablename_id] [INT] IDENTITY(1,1)NOT NULL,

     - 更多的领域在这里

    约束[pk_tablename] PRIMARY KEY
    (
        [tablename_country_id] ASC,
        [tablename_id] ASC
    )
)
 

类文件:

 公共类ModelObject
{
    公共ID {获得;组; }
    //更多的属性在这里
}

公共类ID:INHibernateProxy
{
    公众诠释编号{获得;组; }
    公众诠释CountryId {获得;组; }
    公共ILazyInitializer HibernateLazyInitializer {获得{抛出新ApplicationException的(); }}
}
 

映射文件:

 < XML版本=1.0编码=UTF-8&GT?;
<休眠映射的xmlns =金塔:NHibernate的映射 -  2.2命名空间=模式集会=API>
    <类名=ModelObject表=dbname.dbo.tablename懒惰=假>
        <复合-ID名称=ID级=ID>
            <关键属性名称=ID列=tablename_idTYPE =INT/>
            <关键属性名称=CountryId列=tablename_country_idTYPE =INT/>
        < /复合-ID>
        <! - 更多的特性在这里 - >
    < /类>
< /休眠映射>
 

解决方案

在<著名href="http://stackoverflow.com/questions/1155503/fluent-nhibernate-auto-increment-non-key-id-property">fluent的nhibernate自动增量的非键(ID)属性时,它可能没有必要使用复合密钥,以获得场的产生的性质。考虑使用这种或者类似的:

地图(X => x.Field).COLUMN(田)Generated.Always()只读();

I am working with a legacy database that uses composite keys. And I am trying to use NHibernate to insert a new record into the database. NHibernate specifies that I have to create the Id manually, but when I try to insert with this id I get the message:

System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'tablename' when IDENTITY_INSERT is set to OFF.

I cannot touch any of the db settings as they are administered by head office in USA.

I found that I can do a db insert via:

insert into tablename (tablename_country_id, /*extra fields here*/) values (16, /*extra values here*/)

and the tablename_id column is automatically incremented.

Is it possible to write some sort of a handler that allows me to create an ID object with the CountryId set and have it auto-increment the Id property.

Cheers.


Example Code:

Table Definition:

CREATE TABLE [dbo].[tablename](
    [tablename_country_id] [int] NOT NULL,
    [tablename_id] [int] IDENTITY(1,1) NOT NULL,

    -- more fields here

    CONSTRAINT [pk_tablename] PRIMARY KEY
    (
        [tablename_country_id] ASC,
        [tablename_id] ASC
    )
)

Class Files:

public class ModelObject
{
    public ID { get; set; }
    // more properties here
}

public class ID : INHibernateProxy
{
    public int Id { get; set; }
    public int CountryId { get; set; }
    public ILazyInitializer HibernateLazyInitializer { get { throw new ApplicationException(); } }
}

Mapping File:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model" assembly="API">
    <class name="ModelObject" table="dbname.dbo.tablename" lazy="false">
        <composite-id name="Id" class="ID">
            <key-property name="Id" column="tablename_id" type="int" />
            <key-property name="CountryId" column="tablename_country_id" type="int" />
        </composite-id>
        <!-- more properties here -->
    </class>
</hibernate-mapping>

解决方案

as noted in fluent nhibernate auto increment non key (Id) property, it may not be necessary to use a composite key to get the generated nature of the field. Consider using this or similar to:

Map(x => x.Field).Column("Field").Generated.Always().ReadOnly();

这篇关于插入一条记录使用NHibernate的复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆