C#4.0 / EF - 服务器生成的密钥和服务器生成的值不是由SQL Server精简支持 [英] C# 4.0/EF - Server-generated keys and server-generated values are not supported by SQL Server Compact
问题描述
我刚刚搬进了我的一个项目到VS2010 / fx4.0和正在使用SQL CE数据库作为后备存储。因为它移动到这个版本的.NET现在我得到这个错误:
I have just moved one of my projects into VS2010/fx4.0 and am using a SQL CE database as the backing store. Since moving it to this version of .NET I am now getting this error:
服务器生成的密钥和服务器生成的值不支持通过SQL Server精简。
Server-generated keys and server-generated values are not supported by SQL Server Compact.
我的表 已的用的PK定义用户名(串)及DoorOpen(日期时间)为SQLCE 要求有是在fx3.5每个表的PK。现在,我在fx4.0我难倒。我GOOGLE了,这和每一个答案,我发现是:
My table was defined with a PK of UserName (string) & DoorOpen (datetime) as SQLCE required there be a PK on every table in fx3.5. Now that I am in fx4.0 I am stumped. I've googled for this and every answer I found was:
SQLCE不支持自动生成值(这是我最肯定不会需要),这样就把一个GUID ID在那里,并从代码填充它。
SQLCE does not support auto-generating values (which I am most certainly not needing) so put a GUID ID on there and populate it from code.
我试过这种方法,我仍然得到同样的错误!
I tried this approach and I am still getting the same error!
SQLCE:
CREATE TABLE [ImportDoorAccesses] (
[RawData] nvarchar(100) NOT NULL,
[DoorOpen] datetime NOT NULL,
[UserName] nvarchar(100) NOT NULL,
[CardNumber] bigint NOT NULL,
[Door] nvarchar(4000) NOT NULL,
[Imported] datetime NOT NULL,
[ID] uniqueidentifier NOT NULL -- new column
);
ALTER TABLE [ImportDoorAccesses]
ADD CONSTRAINT [PK_ImportDoorAccesses]
PRIMARY KEY ([ID] );
曾经是约束:
The constraint used to be:
ALTER TABLE [ImportDoorAccesses]
ADD CONSTRAINT [PK_ImportDoorAccesses]
PRIMARY KEY ([DoorOpen],[UserName]);
代码:
CODE:
foreach (dto.DoorAudit newDoorAudit in dataTransferObject)
{
if (newDoorAudit.DoInsert)
{
myEntities.AddToImportDoorAccesses(new ImportDoorAccess
{
CardNumber = newDoorAudit.CardNumber,
Door = newDoorAudit.Door,
DoorOpen = newDoorAudit.DoorOpen,
Imported = newDoorAudit.Imported,
RawData = newDoorAudit.RawData,
UserName = newDoorAudit.UserName,
ID = Guid.NewGuid() // LOOK - HERE IT IS AS SUGGESTED!
});
}
}
myEntities.SaveChanges();
那么,现在该怎么办?这是EF4一个错误? ?难道我做错了什么。
So, now what? Is this a bug in EF4? Am I doing something wrong?
TIA
请注意:
通过EDMX文件去(右键单击,开放,XML)我发现我的日期一列被设定的 StoreGeneratedPattern =身份的。
Going through the EDMX file (right-click, open with, XML) I found that one of my date columns was set with StoreGeneratedPattern="Identity".
<EntityType Name="ImportDoorAccesses">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="RawData" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="DoorOpen" Type="datetime" Nullable="false" />
<Property Name="UserName" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="CardNumber" Type="bigint" Nullable="false" />
<Property Name="Door" Type="nvarchar" Nullable="false" />
<Property Name="Imported" Type="datetime" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="ID" Type="uniqueidentifier" Nullable="false" />
</EntityType>
我再切换回漂亮的模型视图,并在我的数据库点击每一个列,以确保这是不会设置。皮塔是肯定的。看起来像一个完美的小工具/加载项需要创建...
I then switched back to the pretty model view and clicked on every single column in my database to make sure this was NOT set. A PITA for sure. Looks like a perfect little tool/add-in needs to be created...
推荐答案
要检查的重要的是EDMX文件,并确保该属性/列没有身份的StoreGeneratedPattern在那里。
The important thing to check is the EDMX file and make sure this property/column doesn't have a StoreGeneratedPattern of identity in there.
这篇关于C#4.0 / EF - 服务器生成的密钥和服务器生成的值不是由SQL Server精简支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!