gpt4 book ai didi

c# - Entity Framework 抛出无法为表中的标识列插入显式值

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:30 25 4
gpt4 key购买 nike

我是 EF 的新手,在尝试将更改保存到我的上下文时,我收到以下错误:

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

DB 上的列是自动增量标识,在 edmx 上,id StoreGeneratedPattern 是标识,默认值是 none,不知道为什么每次我尝试保存组时它总是尝试插入 0,有什么想法吗?

谢谢

最佳答案

StoreGeneratedPattern 注释必须出现在 EDMX 文件的 两个 位置 - 在 SSDL 部分...

<edmx:StorageModels>
...
<EntityType Name="MyEntity">
...
<Property Name="MyEntityID" Type="int" Nullable="false"
StoreGeneratedPattern="Identity" />
...
</EntityType>
...
</edmx:StorageModels>

...以及在 CSDL 部分:

<edmx:ConceptualModels>
...
<EntityType Name="MyEntity">
...
<Property Name="MyEntityID" Type="Int32" Nullable="false"
annotation:StoreGeneratedPattern="Identity" />
...
</EntityType>
...
</edmx:ConceptualModels>

您的问题的原因可能是两个地方之一缺少注释。

关于c# - Entity Framework 抛出无法为表中的标识列插入显式值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11172188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com