5 回答

TA貢獻1818條經驗 獲得超7個贊
該錯誤表明您無法在 IDENTITY 列上插入數據,因為這樣做已被禁用。
您可以通過執行以下操作來啟用此行為
SET IDENTITY_INSERT [ [ database_name . ] schema_name . ] table_name { ON | OFF }
當您使用實體框架核心時,您必須:
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.DestuffedContainer ON");
context.SaveChanges();
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.DestuffedContainer OFF");
> SQL Server IDENTITY 列中的顯式值
編輯我很抱歉我無法理解您沒有提供價值。
然后確保您的列被標記為模型的 IDENTITY。我看到您正在使用屬性來告訴 efcore 如何命名您的表。只要您不使用Id關鍵屬性的常規名稱,您就需要在實體類上使用[Key]屬性來明確它,在屬性之上,例如:
[Key]
public long DestuffedContainerId { get; set; }
或者在上下文類中使用 FluentAPI:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
?...
? ? modelBuilder
? ? .Entity<DestuffedContainer>()
? ? .Property(d => d.DestuffedContainerId)
? ? .ValueGeneratedOnAdd()?
? ? modelBuilder
? ? .Entity<DestuffedContainer>()
? ? .HasKey(d => d.DestuffedContainerId)
?...
}
還要檢查 sql server 表定義并確保以您的屬性命名的字段設置為IDENTITY,考慮到您收到的錯誤,這是肯定的,但值得檢查。

TA貢獻1810條經驗 獲得超5個贊
對于未來的讀者。
我遇到了相反的問題。我不希望 IDENTITY 自動開啟。
所以像這樣:(非常簡單的流暢映射)
????????builder.HasKey(ent?=>?ent.MyColumnWhichIsTheKey);
我必須添加這一行(ValueGenerateNever)以保持 IDENITY 處于關閉狀態(故意“關閉”):
????????builder.HasKey(ent?=>?ent.MyColumnWhichIsTheKey); ????????builder.Property(ent?=>?ent.MyColumnWhichIsTheKey).ValueGeneratedNever();
其他提示:
using?Microsoft.EntityFrameworkCore;using?Microsoft.EntityFrameworkCore.Metadata.Builders;
和
<PackageReference?Include="Microsoft.EntityFrameworkCore"?Version="3.1.3"?/> <PackageReference?Include="Microsoft.EntityFrameworkCore.Relational"?Version="3.1.3"?/>

TA貢獻1998條經驗 獲得超6個贊
以防萬一有人在 2021 年使用 EF Core 5.x 發現這個問題,我為此苦苦掙扎了幾個小時,經過大量搜索后沒有找到我的解決方案。
就我而言,我有一個現有數據庫,并使用 EF Core 腳手架命令為我創建數據庫上下文和關聯的模型類。當嘗試將新的 TimeEntry 插入數據庫中的 TimeEntry 表時,如下所示:
TimeEntry timeEntry = new TimeEntry
{
EmployeeId = Emp.Id,
ScheduleTypeId = SchedTypeId,
FacilityId = FacilityId,
DateTimeIn1 = DateTimeIn1,
DateTimeIn2 = DateTimeIn2,
DateTimeIn3 = DateTimeIn3,
DateTimeIn4 = DateTimeIn4,
DateTimeIn5 = DateTimeIn5,
DateTimeIn6 = DateTimeIn6,
DateTimeIn7 = DateTimeIn7,
DateTimeOut1 = DateTimeOut1,
DateTimeOut2 = DateTimeOut2,
DateTimeOut3 = DateTimeOut3,
DateTimeOut4 = DateTimeOut4,
DateTimeOut5 = DateTimeOut5,
DateTimeOut6 = DateTimeOut6,
DateTimeOut7 = DateTimeOut7,
Day1TotalHours = day1TotalHoursDecimal,
Day2TotalHours = day2TotalHoursDecimal,
Day3TotalHours = day3TotalHoursDecimal,
Day4TotalHours = day4TotalHoursDecimal,
Day5TotalHours = day5TotalHoursDecimal,
Day6TotalHours = day6TotalHoursDecimal,
Day7TotalHours = day7TotalHoursDecimal,
WeekStartDate = StartDateForWeek,
WeekTotalHours = WeekTotalHoursDecimal
};
db.TimeEntries.Add(timeEntry);
db.SaveChanges();
盡管我沒有明確嘗試插入 ID,但我還是收到了此錯誤。經過多次嘗試,我終于找到了解決方法,嘗試了我能找到的每一篇文章中的所有內容,但沒有任何效果。此代碼解決了我的 DbContext 類中 id 字段的問題:
modelBuilder.Entity<TimeEntry>(entity =>
{
entity.ToTable("TimeEntry");
entity.Property(e => e.Id).HasColumnName("id").ValueGeneratedOnAdd(); // <-- This fixed it for me
另請注意,在其他帖子之后,我也在我的 TimeEntry 模型類中進行了此設置,不確定是否重要以避免此錯誤:
public partial class TimeEntry
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; } // rest of code removed for brevity
我真的希望這對未來的人有所幫助,這讓我發瘋。

TA貢獻1836條經驗 獲得超5個贊
有時,當開發人員在 Context 中手動創建 modelBuilder 時,他/她可能會編寫這樣的代碼,并且準確地 .ValueGenerateNever() -這部分返回您主題中的錯誤
entity.Property(e => e.Id).ValueGeneratedNever();

TA貢獻1804條經驗 獲得超2個贊
如果您想保持 Identity_Insert 關閉并允許在定義的應用程序上下文上的 OnModelCreating 方法中手動插入,則這是一個比所選答案更好的解決方案
這是使用 EF Core for NET 5.0
modelBuilder.Entity<EntityType>().HasKey(x => x.KeyWithIdentityValue); modelBuilder.Entity<EntityType>().Property(x => x.KeyWithIdentityValue).ValueGeneratedOnAdd();```
- 5 回答
- 0 關注
- 266 瀏覽
添加回答
舉報