Visual Studio 2010 +实体框架4:SaveChanges上的UpdateException

时间:2011-06-29 18:34:51

标签: .net visual-studio-2010 entity-framework

经过大量研究并与微软就测试未发布的VS2010 SP1修补程序(我认为我遇到过这个错误:http://connect.microsoft.com/VisualStudio/feedback/details/505178/storegeneratedpattern-property-in-ado-net-entity-model-designer-sets-cdsl-annotation-but-not-ssdl-attribute)进行了联系,我想我会与你分享这个。

本质上,我的实体模型设计器似乎运行良好,因为它将Identity添加为我的ID属性的StoredGeneratedPattern字段的正确值(在edmx文件的SSDL和CSDL区域中)但是当我调用context.SaveChanges时()从我创建的一个简单的应用程序来测试EF设置,我得到一个UpdateException,带有以下InnerException(在UpdateException对象中任何地方都可以找到唯一有意义的文本):

{“不支持将主键列的属性'StoreGeneratedPattern'设置为'Computed'的表的修改。请改用'Identity'模式。键列:'Id'。表:'ConfigurationManagerServiceEntityDataModel.Store .ServiceConfigurationItems'。“}

然而,这根本不可能,因为在整个解决方案中无法找到Computed。

我已经多次从模型中重新生成数据库并检查SQL以及SQL Server Management Studio下的结果表结构和列属性,一切看起来都很好。

为了进一步参考,以下是异常似乎引用的特定模型对象edmx规范:

SSDL:

<EntityType Name="ServiceConfigurationItems">
    <Key>
      <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
    <Property Name="ItemName" Type="nvarchar(max)" Nullable="false" />
    <Property Name="ParentCategoryName" Type="nvarchar(max)" Nullable="false" />
    <Property Name="VicimusService_Id" Type="int" Nullable="false" />
</EntityType>

CSDL:

<EntityType Name="ServiceConfigurationItem">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Type="Int32" Name="Id" Nullable="false" a:StoreGeneratedPattern="Identity" xmlns:a="http://schemas.microsoft.com/ado/2009/02/edm/annotation" />
          <Property Type="String" Name="ItemName" Nullable="false" />
          <Property Type="String" Name="ParentCategoryName" Nullable="false" />
          <NavigationProperty Name="VicimusService" Relationship="ConfigurationManagerServiceEntityDataModel.VicimusServiceConfigurationItem" FromRole="ConfigurationItem" ToRole="VicimusService" />
          <NavigationProperty Name="ConfigurationItemDetails" Relationship="ConfigurationManagerServiceEntityDataModel.ServiceConfigurationItemConfigurationItemDetails" FromRole="ServiceConfigurationItem" ToRole="ConfigurationItemDetails" />
        </EntityType>

此表的SQL生成如下:

-- Creating table 'ServiceConfigurationItems'
CREATE TABLE [dbo].[ServiceConfigurationItems] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ItemName] nvarchar(max)  NOT NULL,
    [ParentCategoryName] nvarchar(max)  NOT NULL,
    [VicimusService_Id] int  NOT NULL
);
GO

此时,我不知道问题可能是什么。

非常感谢任何帮助!

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

好吧,我从头开始再做一遍,这个问题似乎不再发生了。

感谢所有花时间阅读此内容的人!