【问题标题】:Blazor - The INSERT statement conflicted with the FOREIGN KEY constraintBlazor - INSERT 语句与 FOREIGN KEY 约束冲突
【发布时间】:2020-07-27 21:44:40
【问题描述】:

我在外键表上收到冲突错误消息,但是,下拉菜单填充了课程类别 ID,一个 INT 字段作为数字,但该列绑定在 InputText 上,作为字符串而不是 InputNumber ,虽然 io 尝试在剃须刀页面上使用 InputNumber 数据类型,但它不起作用。看起来数字正在转换为字符串,因此出现错误或者我在这里做错了什么?我能够在没有下拉菜单的情况下手动输入字段并且它可以工作,这就是为什么我认为数字正在转换为字符串。

错误

System.Data.SqlClient.SqlException (0x80131904):INSERT 语句与 FOREIGN KEY 约束“FK_Course_CourseCategory”冲突。冲突发生在数据库“ITMS”、表“dbo.CourseCategory”、列“CourseCategoryID”中

剃刀页面

 <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>
     &nbsp;<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" />
    &nbsp;<ValidationMessage For="@(() => newPerson.CourseCategoryID)" />
</div>

型号

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

【问题讨论】:

  • ntexttextimage 数据类型将在 SQL Server 的未来版本中删除。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。请改用nvarchar(max)varchar(max)varbinary(max)See details here
  • 代码和图片太多了,仍然缺少最相关的部分(OnModelCreating ?)。标签也是如此。这不是关于 Blazor 或 Razor,而是关于 EntityFramework(我猜)。

标签: c# asp.net razor blazor


【解决方案1】:

InputSelect 不支持绑定到整数: https://github.com/dotnet/aspnetcore/blob/master/src/Components/Web/src/Forms/InputSelect.cs

使用&lt;select&gt; 标签代替InputSelect 喜欢

<select @bind="model.ByCountryId">
    @if (model?.Countries != null)
    {
        @foreach (var cnt in model.Countries)
        {
            <option value="@cnt.Id">@cnt.Name</option>
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多