C# 4.0/EF - SQL Server Compact 不支持服务器生成的键和服务器生成的值

发布于 2024-08-30 07:39:38 字数 2625 浏览 4 评论 0原文

我刚刚将我的一个项目转移到 VS2010/fx4.0 中,并使用 SQL CE 数据库作为后备存储。自从将其移至此版本的 .NET 后,我现在收到此错误:

SQL Server Compact 不支持服务器生成的键和服务器生成的值。

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

SQLCE 不支持自动生成值(我肯定不需要),因此在其中放置一个 GUID ID 并从代码中填充它。

我尝试了这种方法,但仍然遇到同样的错误!

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 中的错误吗?我做错了什么吗?

TIA


注意:

浏览 EDMX 文件(右键单击,使用 XML 打开),我发现我的日期列之一是使用 StoreGeneratePattern="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 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:

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

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

So, now what? Is this a bug in EF4? Am I doing something wrong?

TIA


NOTE:

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...

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

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

发布评论

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

评论(1

你与昨日 2024-09-06 07:39:38

要检查的重要事项是 EDMX 文件,并确保此属性/列中没有 StoreGeneratePattern 标识。

The important thing to check is the EDMX file and make sure this property/column doesn't have a StoreGeneratedPattern of identity in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文