繁体   English   中英

EF Core 2:MERGE 语句与 FOREIGN KEY 约束冲突

[英]EF Core 2: The MERGE statement conflicted with the FOREIGN KEY constraint

我有以下课程:

public class GSProject
{
    public int Id { get; set; }
    public ICollection<GSPoint> Points { get; set; }
    public ICollection<GSPlanarSurface> PlanarSurfaces { get; set; }
}

public class GSPoint
{
    public int Id { get; set; }
    public int? PlanarSurfaceId { get; set; }
    public GSPlanarSurface PlanarSurface { get; set; }
}

public class GSPlanarSurface
    {
        public int Id { get; set; }

        public ICollection<GSPoint> Points { get; set; }

        public int? PxyId { get; set; }
        public GSPoint Pxy { get; set; }

        public int? PyId { get; set; }
        public GSPoint Py { get; set; }
}

和上下文类(OnModelCreating() 相关内容):

modelBuilder.Entity<GSPlanarSurface>()
                .HasMany(e => e.Points)
                .WithOne(e => e.PlanarSurface)
                .HasForeignKey(e => e.PlanarSurfaceId);

然后我创建了 ef 迁移和相关的迁移 .cs 文件内容是:

migrationBuilder.CreateTable(
                name: "GSPoints",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    PlanarSurfaceId = table.Column<int>(nullable: true),
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GSPoints", x => x.Id);
                    table.ForeignKey(
                        name: "FK_GSPoints_GSPlanarSurfaces_PlanarSurfaceId",
                        column: x => x.PlanarSurfaceId,
                        principalTable: "GSPlanarSurfaces",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

错误:
{System.Data.SqlClient.SqlException (0x80131904):MERGE 语句与 FOREIGN KEY 约束“FK_GSPoints_GSPlanarSurfaces_PlanarSurfaceId”冲突。 冲突发生在数据库“GSTDb”、表“dbo.GSPlanarSurfaces”、“Id”列中。

如果我尝试调用,则会出现:

context.Set<GSProject>().Add(entity);
await context.SaveChangesAsync();

entity 是 GSProject 的一个实例,有几个 GSPoints,没有任何 GSPlanarSurface 实例(entity.PlanarSurfaces Count = 0)

编辑:
这是在 t-sql 中为密钥自动生成的脚本:

USE [GSTDb]
GO

ALTER TABLE [dbo].[GSPoints]  WITH CHECK ADD  CONSTRAINT [FK_GSPoints_GSPlanarSurfaces_PlanarSurfaceId] FOREIGN KEY([PlanarSurfaceId])
REFERENCES [dbo].[GSPlanarSurfaces] ([Id])
GO

ALTER TABLE [dbo].[GSPoints] CHECK CONSTRAINT [FK_GSPoints_GSPlanarSurfaces_PlanarSurfaceId]
GO


我执行了简单的 tsql 脚本:

insert into GSProjects ([Description] ,[Name]) values ('d1', 'proj1')
insert into GSPoints([Number], [IsSlave] ,[X], [Y], [Z]) values (0, 0, 0, 0, 0)
insert into GSPoints([GSProjectId], [Number], [IsSlave] ,[X], [Y], [Z]) values (5, 0, 0, 0, 0, 0)


并且插入没有问题。 第一个 GSPoint 的 GSProjectId 设置为 null,第二个 GSPoint 的 GSProjectId 设置为非空值(此处为 5)。 两个都将 GSPlanarSurfaceId 设置为 null 并且都正确插入。

答案与 EntityFramework 无关。 在我的情况下,PlanarSurfaceId 默认设置为 0。我将其更改为 null 并且它可以工作。

我遇到了类似的问题,我从 excel 表中对 sql 实现了“导入”功能,其中所有数据都是“通用名称”,当通过 firstordefault() 找到特定列的正确 ID 的引用时,结果为 0 - 未找到,所以在尝试保存上下文,我收到此错误。 即使我尝试删除整个表数据,我也遇到了同样的错误,而另一个表保留了主表使用的 ID。 我希望这对某些人有所帮助,我花了很多时间对此进行调试,因为 10 行的测试导入很好,并且满(36k 行)一遍又一遍地向我抛出此错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM