[英]BulkInsert: The INSERT statement conflicted with the FOREIGN KEY constraint
[英]Blazor - The INSERT statement conflicted with the FOREIGN KEY constraint
我在我的外鍵表上收到了沖突的錯誤消息,但是,下拉菜單填充了課程類別 ID,一個 INT 字段作為一個數字,但該列綁定在 InputText 上,作為一個字符串而不是 InputNumber,盡管 io嘗試在 razor 頁面上使用 InputNumber 數據類型,但沒有用。 看起來數字正在轉換為字符串,因此出現錯誤或者我在這里做錯了什么? 我能夠在沒有下拉菜單的情況下手動輸入字段並且它有效,這就是為什么我認為數字正在轉換為字符串。
錯誤
System.Data.SqlClient.SqlException (0x80131904):INSERT 語句與 FOREIGN KEY 約束“FK_Course_CourseCategory”沖突。 沖突發生在數據庫“ITMS”、表“dbo.CourseCategory”、“CourseCategoryID”列中
Razor 頁
<div class="col-12 row">
<label class="col-2 font-weight-bold">Course Title:</label>
<InputSelect @bind-Value="@newPerson.Course">
<option value="0">Select</option>
@foreach (var item in CourseCategories)
{
<option value="@item.CourseCategoryID">@item.Title</option>
}
</InputSelect>
<ValidationMessage For="@(() => newPerson.Course)" />
</div>
<div class="col-12 row">
<label class="col-2 font-weight-bold">Course Category ID:</label>
<InputText id="CourseCategoryID" @bind-Value="newPerson.Course" placeholder="CourseCategoryID" />
<ValidationMessage For="@(() => newPerson.CourseCategoryID)" />
</div>
Model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace BlazorDemoUI.Models
{
public class DisplaySchoolModel
{
public string CountryName { get; set; }
[Required]
public int SchoolID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Location { get; set; }
[Required]
public string Address { get; set; }
[Required]
public string PostCode { get; set; }
public string CountryCode { get; set; }
[Required]
public int SchoolAdminPersonID { get; set; }
}
}
表:
CREATE TABLE [dbo].[CourseCategory]
(
[CourseCategoryID] [int] IDENTITY(1,1) NOT NULL,
[Code] [nvarchar](50) NOT NULL,
[Title] [nvarchar](50) NOT NULL,
[Summary] [ntext] NULL,
[Description] [ntext] NULL,
[Notes] [ntext] NULL,
[Duration] [smallint] NULL,
CONSTRAINT [PK_CourseCategory]
PRIMARY KEY CLUSTERED ([CourseCategoryID] ASC)
) ON [PRIMARY]
CREATE TABLE [dbo].[Course]
(
[CourseID] [int] IDENTITY(1,1) NOT NULL,
[CourseCategoryID] [int] NOT NULL,
[SchoolID] [int] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Course] [nvarchar](50) NOT NULL,
[StartDate] [smalldatetime] NOT NULL,
[Duration] [int] NOT NULL,
[Seats] [int] NOT NULL,
[Notes] [ntext] NULL,
[PublicClass] [bit] NOT NULL,
[SeatsAvailable] [int] NOT NULL,
[Instructor] [nvarchar](50) NULL,
[IsCancelled] [bit] NOT NULL,
[ReasonForChange] [nvarchar](250) NULL,
[HasPrerequisite] [bit] NOT NULL,
[PrerequisiteName] [nvarchar](100) NULL,
CONSTRAINT [PK_Course]
PRIMARY KEY CLUSTERED ([CourseID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Course] WITH CHECK
ADD CONSTRAINT [FK_Course_CourseCategory]
FOREIGN KEY([CourseCategoryID]) REFERENCES [dbo].[CourseCategory] ([CourseCategoryID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Course] CHECK CONSTRAINT [FK_Course_CourseCategory]
GO
ALTER TABLE [dbo].[Course] WITH CHECK
ADD CONSTRAINT [FK_Course_School]
FOREIGN KEY([SchoolID]) REFERENCES [dbo].[School] ([SchoolID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Course] CHECK CONSTRAINT [FK_Course_School]
GO
InputSelect
不支持綁定到 integer: https://github.com/do.net/as.netcore/blob/master/src/Components/Web/src/Forms/InputSelect.cs
使用<select>
標簽而不是InputSelect
類的
<select @bind="model.ByCountryId">
@if (model?.Countries != null)
{
@foreach (var cnt in model.Countries)
{
<option value="@cnt.Id">@cnt.Name</option>
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.