使用guid主键忽略LINQ to SQL实体列名称属性 [英] LINQ to SQL entity column name attribute ignored with guid primary key
问题描述
我正在使用带有LINQ to SQL(SQL Server 2005 SP3 x64)的简单实体类.
I was working with a simple entity class with LINQ to SQL (SQL Server 2005 SP3 x64).
[Table( Name="TBL_REGISTRATION" )]
public sealed class Registration : IDataErrorInfo
{
[Column( Name = "TBL_REGISTRATION_PK", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert )]
public Guid RegistrationID { get; private set; }
/* other properties ommited for brevity */
}
这里只有两点有趣的事情:
There are only two somewhat interesting things here:
- 类名和属性名与表名和列名不同
- 主键是Guid(唯一标识符)
这是桌子的样子:
create table dbo.TBL_REGISTRATION
(
TBL_REGISTRATION_PK uniqueidentifier primary key clustered
rowguidcol
default newid(),
/* other columns ommited for brevity */
)
当我将此实体附加到表并在DataContext上提交更改时,LINQ堆栈会抛出SqlException:
When I attach this entity to my table and submit changes on my DataContext, the LINQ stack throws back a SqlException:
SqlException(0x80131904):无效的列名'RegistrationID'
SqlException (0x80131904): Invalid column name 'RegistrationID'
LINQ似乎忽略了我的RegistrationID属性上的Column(Name ="TBL_REGISTRATION_PK")属性.我花了一些时间来装饰各种属性装饰,以使其正常工作.最后,我决定使用私有的TBL_REGISTRATION_PK属性包装我的RegistrationID属性,以使LINQ满意.
LINQ seems to be ignoring the Column( Name = "TBL_REGISTRATION_PK" ) attribute on my RegistrationID property. I spent a while futzing with different attribute decorations trying to get it to work. In the end I settled on a private TBL_REGISTRATION_PK property to wrap my RegistrationID property to make LINQ happy.
[Table( Name="TBL_REGISTRATION" )]
public sealed class Registration : IDataErrorInfo
{
public Guid RegistrationID { get; private set; }
[Column( IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert )]
private Guid TBL_REGISTRATION_PK { get { return RegistrationID; } set { RegistrationID = value; } }
/* other properties ommited for brevity */
}
这有效.
为什么第一种方法不起作用?我在这里做错了还是这是LINQ缺陷?
Why didn't it work the first way? Am I doing something wrong here or is this a LINQ defect?
推荐答案
这是Linq-to-SQL中的错误.它在.net 4.0中已修复.
This is a bug in Linq-to-SQL. It is fixed in .net 4.0.
请参阅Connect#381883: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx ?FeedbackID = 381883
See Connect #381883: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=381883
这篇关于使用guid主键忽略LINQ to SQL实体列名称属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!