我们正在使用.NET Core和带有Code First的EF Core开发应用程序,因此我们正在使用Migrations创建和扩展数据库。
该应用程序将用于设置约定。因此,有演讲者,会议和不同主题。
我有两个实体。 WishlistSpeaker和Track,它们形成多对多关系。使用Fluent API,我使用两个表中的ID创建了一个联接表。现在,当我创建带有曲目列表的wishlistSpeaker时,它会整齐地存储在WishlistspeakerTrack连接表中。当我尝试更新该WishlistSpeaker并添加或删除曲目时,扬声器和WishlistSpeakerTracks均被删除。我在调试中没有看到任何错误。

我尝试在迁移中更改onDelete和OnUpdate操作,然后重新应用它们。看到我的代码中没有错误,我认为这与数据库有关,所以我花了大部分时间在数据库中搜索。

WishlistSpeaker实体

    public class WishlistSpeaker
    {
        [Key]
        public int WishlistSpeakerId { get; set; }

        public string FirstName { get; set; }

        public string Name { get; set; }

        public int EventID { get; set; }

        public string Company { get; set; }

        public string Mail { get; set; }

        public string Country { get; set; }

        public string Twitter { get; set; }

        public List<WishlistSpeakerTrack> WishlistSpeakerTracks { get; set; }

        public int StatusId { get; set; }

        public Status Status { get; set; }

        public string LinkedIn { get; set; }
    }


追踪实体

    public class Track
    {
        [Key]
        public int TrackId { get; set; }

        [Required]
        public string TrackName { get; set; }

        public int EventId { get; set; }

        public Event Event { get; set; }

        public List<WishlistSpeakerTrack> WishlistSpeakerTracks { get; set; }
    }


这是为JoinTable创建的实体

    public class WishlistSpeakerTrack
    {
        [Key]
        public int TrackId { get; set; }

        public Track Track { get; set; }

        [Key]
        public int WishlistSpeakerId { get; set; }

        public WishlistSpeaker WishlistSpeaker { get; set; }
    }


这是在迁移中创建的。现在将OnDelete设置为NoAction,但它以前是Restrict

 migrationBuilder.CreateTable(
                name: "WishlistSpeakerTrack",
                columns: table => new
                {
                    TrackId = table.Column<int>(nullable: false),
                    WishlistSpeakerId = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WishlistSpeakerTrack", x => new { x.TrackId, x.WishlistSpeakerId });
                    table.ForeignKey(
                        name: "FK_WishlistSpeakerTrack_Track_TrackId",
                        column: x => x.TrackId,
                        principalTable: "Track",
                        principalColumn: "TrackId",
                        onDelete: ReferentialAction.NoAction);
                    table.ForeignKey(
                        name: "FK_WishlistSpeakerTrack_WishlistSpeakers_WishlistSpeakerId",
                        column: x => x.WishlistSpeakerId,
                        principalTable: "WishlistSpeakers",
                        principalColumn: "WishlistSpeakerId",
                        onDelete: ReferentialAction.NoAction);
                });


我不会更新我的WishlistSpeaker和JoinTable而不删除它们。

最佳答案

我两者都做,但都没有。
我想我找到了问题。在我的ContextModelSnapshot中,它仍然设置为onDelete: ReferentialAction.Cascade
我该如何更改?

modelBuilder.Entity("Oganize.Api.Core.Entities.WishlistSpeakerTrack", b =>
                {
                    b.HasOne("Oganize.Api.Core.Entities.Track", "Track")
                        .WithMany("WishlistSpeakerTracks")
                        .HasForeignKey("TrackId")
                        .OnDelete(DeleteBehavior.Cascade);

                    b.HasOne("Oganize.Api.Core.Entities.WishlistSpeaker", "WishlistSpeaker")
                        .WithMany("WishlistSpeakerTracks")
                        .HasForeignKey("WishlistSpeakerId")
                        .OnDelete(DeleteBehavior.Cascade);
                });

10-06 12:00