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

时间:2018-09-12 09:49:10

标签: c# tsql entity-framework-core .net-core-2.0

我有以下课程:

<addForeignKeyConstraint
            constraintName="FK_ADDRESS_PERSON_ADDRESS"
            referencedTableName="ADDRESS"
            baseColumnNames="ADDRESS_ID"
            baseTableName="PERSON_ADDRESS" referencedColumnNames="id" />

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

<addForeignKeyConstraint
            constraintName="FK_PERSON_PERSON_ADDRESS"
            referencedTableName="PERSON"
            baseColumnNames="PERSON_ID"
            baseTableName="PERSON_ADDRESS" referencedColumnNames="id" />

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

    function saveSelection() {
        var range = null;
        if (window.getSelection) {
            sel = window.getSelection();
            if (sel.getRangeAt && sel.rangeCount) {
               return sel.getRangeAt(0);
            }
        } else if (document.selection && document.selection.createRange) {
            return document.selection.createRange();
        }
        return null;
    }

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

如果我尝试致电会出现:

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; }
}

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

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

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


我执行了简单的tsql脚本:

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);
                });


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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我有类似的问题,我从excel表中向SQL实现了“导入”功能,其中所有数据均为“通用名称”,当通过firstordefault()找到对特定列的正确ID的引用时,结果为0-未找到,因此在保存上下文的尝试中,出现了此错误。即使试图删除整个表数据,我也会遇到相同的错误,而另一个表保留了主表使用的ID。我希望这对某人有所帮助,我花了很多时间调试它,因为10行的测试导入效果很好,而完整(36,000行)会反复向我抛出此错误。