C# 4.0/EF - SQL Server Compact 不支持服务器生成的键和服务器生成的值 [英] C# 4.0/EF - Server-generated keys and server-generated values are not supported by SQL Server Compact

查看:17
本文介绍了C# 4.0/EF - 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 Compact 不支持服务器生成的键和服务器生成的值.

Server-generated keys and server-generated values are not supported by SQL Server Compact.

我的表用用户名(字符串)&的PK定义的DoorOpen(日期时间)作为 SQLCE 必需 在 fx3.5 中的每个表上都有一个 PK.现在我在 fx4.0 中,我被难住了.我用谷歌搜索过这个,我找到的每个答案都是:

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] );

过去的约束是:

ALTER TABLE [ImportDoorAccesses]
ADD CONSTRAINT [PK_ImportDoorAccesses]
    PRIMARY KEY ([DoorOpen],[UserName]);

代码:

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="Identity".

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 Compact 不支持服务器生成的键和服务器生成的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆