使用 FluentNHibernate 组合键中的身份字段

发布于 2024-12-14 10:02:00 字数 1159 浏览 4 评论 0原文

我有一张带有组合键的表,其中一部分是身份字段。 Identity字段显然是后来添加的,因为有超过1000处重复的位置,因此依赖组合键的第二部分来提供唯一性。下面是表声明:

CREATE TABLE [dbo].[tblSaveCommissions](
    [SaveTransID] [int] NOT NULL,
    [CommID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [AccountNumber] [varchar](40) NULL,
    ... (extra fields)
 CONSTRAINT [PK_tblSaveCommissions] PRIMARY KEY CLUSTERED 
(
    [SaveTransID] ASC,
    [CommID] ASC)
)

FluentNHibernate 映射如下所示(缩写):

public class SaveCommissionMap : ClassMap<SaveCommissionEntity>
{
    public SaveCommissionMap()
    {
        Table("tblSaveCommissions");

        CompositeId()
            .KeyProperty(x => x.Id, "CommID")
            .KeyProperty(x => x.SaveTransactionId, "SaveTransId");

        Map(x => x.AccountNumber);

        References(x => x.SaveTransaction)
            .Column("SaveTransId").Not.Insert().Not.Update();
    }

现在,当我尝试插入一条记录时,出现以下错误:

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

如何使用映射来使其工作?

I have a table with a composite key, one part of which is an Identity field. The Identity field was obviously added later, because there are over 1000 places where the identity is duplicated, thus relying on the second part of the composite key to provide uniqueness. Here is the table declaration:

CREATE TABLE [dbo].[tblSaveCommissions](
    [SaveTransID] [int] NOT NULL,
    [CommID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [AccountNumber] [varchar](40) NULL,
    ... (extra fields)
 CONSTRAINT [PK_tblSaveCommissions] PRIMARY KEY CLUSTERED 
(
    [SaveTransID] ASC,
    [CommID] ASC)
)

The FluentNHibernate mapping looks like this (abbreviated):

public class SaveCommissionMap : ClassMap<SaveCommissionEntity>
{
    public SaveCommissionMap()
    {
        Table("tblSaveCommissions");

        CompositeId()
            .KeyProperty(x => x.Id, "CommID")
            .KeyProperty(x => x.SaveTransactionId, "SaveTransId");

        Map(x => x.AccountNumber);

        References(x => x.SaveTransaction)
            .Column("SaveTransId").Not.Insert().Not.Update();
    }

Now when I try to insert a record, I get the following error:

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

How can I work the mapping to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2024-12-21 10:02:00

另一种可能性是使 SaveCommission 成为 SaveTransaction 的依赖组件。

class SaveTransaction
{
    public virtual IList<SaveCommission> Commissions { get; set; }
}

class SaveTransactionMap : ClassMap<SaveTransaction>
{
    public SaveTransactionMap()
    {
        HasMany(x => x.Commissions)
            .KeyColumn("SaveTransID")
            .AsBag()
            .OrderBy("CommID")
            .Component(c => 
            {
                c.ParentReference(x => x.SaveTransaction);
                c.Map(x => x.AccountNumber);
            })
    }
}

some alternate possibility is to make SaveCommission a dependant component of SaveTransaction.

class SaveTransaction
{
    public virtual IList<SaveCommission> Commissions { get; set; }
}

class SaveTransactionMap : ClassMap<SaveTransaction>
{
    public SaveTransactionMap()
    {
        HasMany(x => x.Commissions)
            .KeyColumn("SaveTransID")
            .AsBag()
            .OrderBy("CommID")
            .Component(c => 
            {
                c.ParentReference(x => x.SaveTransaction);
                c.Map(x => x.AccountNumber);
            })
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文