【问题标题】:nhibernate issue with "Cannot insert explicit value for identity column in table"“无法为表中的标识列插入显式值”的休眠问题
【发布时间】:2011-05-05 02:30:05
【问题描述】:

我似乎在使用 nHibernate 和插入记录时遇到了随机问题。

插入工作正常,然后添加了一个新表,并开始收到错误:

Cannot insert explicit value for identity column in table '' when IDENTITY_INSERT is set to OFF

现有的表插入现在也失败了。随机部分是,如果我删除我的文件并重新签出,那么插入就可以正常工作。

nHibernate 版本是 3.1

身份列正在通过以下方式设置:

[NHibernate.Mapping.Attributes.Generator(Class = "native")]
[NHibernate.Mapping.Attributes.Id(Name = "id")]
public virtual IdT id
{
    get { return _id; }
    set { _id = value; }
}

我尝试设置为“身份”,但仍然是个问题。不确定是否存在缓存问题,但担心会影响后期的功能。

【问题讨论】:

  • 您能详细说明您添加的新表吗?它是如何映射的?

标签: nhibernate nhibernate-mapping


【解决方案1】:

遇到了这个错误。我立即意识到这是一个身份问题。但我设置了自动增量。所以我检查了我的映射文件。我很快意识到:

    <id name="Id" access="property" column="`id`">
        <generator class="assigned" />
    </id>

必须是

    <id name="Id" access="property" column="`id`">
        <generator class="native" />
    </id>

注意: class="native"

【讨论】:

    猜你喜欢
    • 2016-04-13
    • 2015-10-27
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多