首页 > 解决方案 > C# 实体框架错误:“SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_718”冲突

问题描述

我对使用实体框架相当陌生,无法弄清楚这个错误。我正在从 Excel 表中提取数据并将其放入带有 EF 的数据库中。这个问题还有其他帖子,但是在浏览完之后,似乎没有任何效果。

为了结束我的错误陈述,它说:“数据库'DataWarehouse',表'dbo.RVTools_vSwitch',列'vSwitch_id'中发生冲突。” 在跟踪这方面的论文后,我可以确认 dbo.RVTools_vSwitch 表中有一个列“vSwitch_id”。vSwitch 是我的 excel 文件中的一张表,vSwitch_id 是由 EF 自动生成的。

当我尝试从不同的 Excel 表 vPort 导入数据时,我遇到了问题。需要导入数据库的项目之一是 vSwitch_id。所以我有一个 GetID 函数,它使用 excel 表给出的一些参数来找到正确的 ID,但这仍然不起作用。

我尝试的下一件事是查看 vPort 和 vSwitch 的类,其中包含 EF 调用以将数据添加到数据库和迁移以检查所需的一切是否都在那里,但在那里找不到任何问题。

因此,如果我有正确的迁移、正确的类、正确的 excel 数据,并且我的数据库中有正确的表和行,还有什么可能导致此错误?

这是将数据从excel导入数据库的代码。VSwitchId 是错误所在,vPort 是我要导入的工作表:

foreach (DataRow dataRow in Data.Rows)
            {
                context.RvtoolsVPort.Add(new RvtoolsVPort
                {
                    PortGroup = dataRow[0].ToString(),
                    Vlan = Convert.ToInt32(dataRow[1]),
                    PromiscuousMode = Convert.ToBoolean(bool.Parse(dataRow[2].ToString())),
                    MacChanges = Convert.ToBoolean(bool.Parse(dataRow[3].ToString())),
                    ForgedTransmits = Convert.ToBoolean(bool.Parse(dataRow[4].ToString())),
                    TrafficShaping = Convert.ToBoolean(bool.Parse(dataRow[5].ToString())),
                    Width = Convert.ToInt32(dataRow[6]),
                    Peak = Convert.ToInt32(dataRow[7]),
                    Burst = Convert.ToInt32(dataRow[8]),
                    Policy = dataRow[9].ToString(),
                    ReversePolicy = Convert.ToBoolean(bool.Parse(dataRow[10].ToString())),
                    NotifySwitch = Convert.ToBoolean(bool.Parse(dataRow[11].ToString())),
                    RollingOrder = Convert.ToBoolean(bool.Parse(dataRow[12].ToString())),
                    Offload = Convert.ToBoolean(bool.Parse(dataRow[13].ToString())),
                    Tso = Convert.ToBoolean(bool.Parse(dataRow[14].ToString())),
                    ZeroCopyXmit = Convert.ToBoolean(bool.Parse(dataRow[15].ToString())),
                    VSwitchId = vSwitch.GetID(dataRow[19].ToString(), dataRow[16].ToString(), dataRow[18].ToString(), dataRow[17].ToString(), assessment_id),
                    AssessmentId = assessment_id
                });
                context.SaveChanges();
            }

这是 vPort 的类:

namespace KelderModel
{
    public partial class RvtoolsVPort
    {
        public int VPortId { get; set; }
        public string PortGroup { get; set; }
        public int? Vlan { get; set; }
        public bool? PromiscuousMode { get; set; }
        public bool? MacChanges { get; set; }
        public bool? ForgedTransmits { get; set; }
        public bool? TrafficShaping { get; set; }
        public int? Width { get; set; }
        public int? Peak { get; set; }
        public int? Burst { get; set; }
        public string Policy { get; set; }
        public bool? ReversePolicy { get; set; }
        public bool? NotifySwitch { get; set; }
        public bool? RollingOrder { get; set; }
        public bool? Offload { get; set; }
        public bool? Tso { get; set; }
        public bool? ZeroCopyXmit { get; set; }
        public int VSwitchId { get; set; }
        public int AssessmentId { get; set; }
        public virtual Assessment Assessment { get; set; }
        public virtual RvtoolsVSwitch VSwitch { get; set; }
       }
 }

最后,这是我的 vPort 迁移代码:

migrationBuilder.CreateTable(
            name: "RVTools_vPort",
            columns: table => new
            {
                vPort_id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                port_group = table.Column<string>(unicode: false, maxLength: 255, nullable: true),
                vlan = table.Column<int>(nullable: true),
                promiscuous_mode = table.Column<bool>(nullable: true),
                MAC_changes = table.Column<bool>(nullable: true),
                forged_transmits = table.Column<bool>(nullable: true),
                traffic_shaping = table.Column<bool>(nullable: true),
                width = table.Column<int>(nullable: true),
                peak = table.Column<int>(nullable: true),
                burst = table.Column<int>(nullable: true),
                policy = table.Column<string>(unicode: false, maxLength: 255, nullable: true),
                reverse_policy = table.Column<bool>(nullable: true),
                notify_switch = table.Column<bool>(nullable: true),
                rolling_order = table.Column<bool>(nullable: true),
                offload = table.Column<bool>(nullable: true),
                TSO = table.Column<bool>(nullable: true),
                zero_copy_xmit = table.Column<bool>(nullable: true),
                vSwitch_id = table.Column<int>(nullable: false),
                assessment_id = table.Column<int>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_RVTools_vPort", x => x.vPort_id);
                table.ForeignKey(
                    name: "FK_849",
                    column: x => x.assessment_id,
                    principalTable: "Assessment",
                    principalColumn: "assessment_id",
                    onDelete: ReferentialAction.Cascade);
                table.ForeignKey(
                    name: "FK_718",
                    column: x => x.vSwitch_id,
                    principalTable: "RVTools_vSwitch",
                    principalColumn: "vSwitch_id",
                    onDelete: ReferentialAction.Restrict);
            });

迁移过程中会出现一些问题吗?感谢您提供的任何帮助。

标签: c#sql-serverentity-frameworkentity-framework-migrations

解决方案


推荐阅读