我是使用 EF 和 ASP.NET Core 的新手。我正在開發一個簡單的 Web 應用程式,當我保存新記錄時出現錯誤。當呼叫 DbContext.SaveChanges() 時,我收到一條錯誤訊息:
“當 IDENTITY_INSERT 設定為 OFF 時,無法在表 'ContactTypes' 中插入標識列的顯式值。”
我完全理解該訊息的含義以及我收到它的原因。根據該訊息,EF 正在嘗試將記錄插入到 ContactTypes 表中,并且由于該表不允許插入主鍵值,因此會出現該錯誤。ContactTypes 表有一組可能不會改變的固定記錄。ContactTypes 表中的記錄用于填充下拉串列(HTML 選擇元素)。該功能運行良好,當用戶選擇所需選項然后嘗試保存父(客戶端)記錄時,EF 希望將新記錄插入到 ContactTypes 表中,這是不必要的。當 EF Core 創建資料庫時,“ContactTypeID”欄位被添加到 Clients 表中,該欄位應保存所選 ContactType 的 ID。

下面是我的課
public class Client
{
public int ID { get; set; }
public Person Person { get; set; }// = new Person();
public ContactType ContactType { get; set; }// = new ContactType();//Client or Inquiry
//other properties removed for brevity
}
public class ContactType
{
public int ID { get; set; }
[StringLength(20)]
public string ContactTypeDescription { get; set; } //typically either Client or Inquiry
}
public class Person
{
public int ID { get; set; }
[Required, StringLength(50)]
public string FirstName { get; set; } = "N/A";
[Required, StringLength(50)]
public string LastName { get; set; } = "N/A";
//other properties removed for brevity
}
這是我的 OnModelCreating 方法:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//setup our relationships
modelBuilder.Entity<Client>()
.HasOne(c => c.Person);
modelBuilder.Entity<Client>()
.HasOne(c => c.ContactType)
.WithMany();
//create our other supporting tables and initialize data if necessary
modelBuilder.Entity<Person>().ToTable("People");
modelBuilder.Entity<Person>().HasData(new Person { ID = 1, FirstName = "N/A", LastName = "N/A" });
modelBuilder.Entity<ContactType>()
.ToTable("ContactTypes");
modelBuilder.Entity<ContactType>().HasData(new ContactType { ID = 1, ContactTypeDescription = "Not Specified" },
new ContactType { ID = 2, ContactTypeDescription = "Inquiry" },
new ContactType { ID = 3, ContactTypeDescription = "Client" });
}
這是我對 HTML 選擇的標記
<div class="col-sm-3">
<label for="type" class="form-label stacked-justify-left">Contact Type</label>
<select asp-for="Client.ContactType.ID" asp-items="Model.ContactTypes" class="form-control" style="height:40px; width:180px">
<option value="">-- Select Type --</option>
</select>
<span asp-validation-for="Client.ContactType.ID" class="text-danger font-weight-bold"></span>
</div>
所以我不確定在保存客戶端物體資訊時如何告訴 EF Core 不要將記錄添加到 ContactTypes 表中。如果需要其他資訊,請詢問,我會提供。任何幫助表示贊賞。
uj5u.com熱心網友回復:
可能Client類應該是:
public class Client
{
public int ID { get; set; }
public Person Person { get; set; }// = new Person();
public int ContactTypeID { get; set; }
public ContactType ContactType { get; set; }// = new ContactType();//Client or Inquiry
}
public class ContactType
{
public int ID { get; set; }
[StringLength(20)]
public string ContactTypeDescription { get; set; } //typically either Client or Inquiry
public IList<Client> Clients { get; set; }
}
modelBuilder.Entity<Client>()
.HasOne(c => c.ContactType)
.WithMany()
.HasForeignKey(c => c.ContactTypeId);
并將視圖修改為:
<select asp-for="Client.ContactTypeID" asp-items="Model.ContactTypes" class="form-control" style="height:40px; width:180px">
<option value="">-- Select Type --</option>
</select>
<span asp-validation-for="Client.ContactTypeID" class="text-danger font-weight-bold"></span>
參考
- Entity Framework Core 中的約定
- Entity Framework Core 中的一對多關系約定
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/475670.html
上一篇:如何在.NET6中注冊動作過濾器
下一篇:在提交表單之前詢問用戶
