使用 FluentNHibernate 组合键中的身份字段
我有一张带有组合键的表,其中一部分是身份字段。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一种可能性是使 SaveCommission 成为 SaveTransaction 的依赖组件。
some alternate possibility is to make
SaveCommission
a dependant component of SaveTransaction.