Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade delete not working one-to-one #38

Open
joacar opened this issue Dec 8, 2016 · 0 comments
Open

Cascade delete not working one-to-one #38

joacar opened this issue Dec 8, 2016 · 0 comments

Comments

@joacar
Copy link

joacar commented Dec 8, 2016

Hi,

First of, great work!

I have a model roughly

public class Profile
{
// Other properties excluded for brevity
public InstagramChannel Instagram { get; set;}

public BlogChannel Blog { get; set;}
}

public class BlogChannel
{
}

public class InstagramChannel
{

}

In the model builder I configure them like so

modelBuilder.Entity<Profile>()
    .HasOne(profile => profile.Instagram)
    .WithOne(typeof(InstagramChannel), "ProfileId")
    .HasForeignKeyConstraint("FK_Profile_Id")
    .OnDelete(DeleteBehavior.Delete);

This is the generated code from the .cs file

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;

namespace App.Migrations
{
    public partial class _1 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Profiles",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGeneratedOnAdd", true),
                    BlogId = table.Column<int>(nullable: true),
                    City = table.Column<string>(nullable: false),
                    CivilStatus = table.Column<int>(nullable: false),
                    Country = table.Column<string>(nullable: true),
                    DateOfBirth = table.Column<string>(maxLength: 10, nullable: true),
                    Description = table.Column<string>(nullable: true),
                    Email = table.Column<string>(nullable: true),
                    FullName = table.Column<string>(nullable: false),
                    Gender = table.Column<int>(nullable: false),
                    ImageUrl = table.Column<string>(nullable: true),
                    InstagramId = table.Column<string>(nullable: true),
                    Mobile = table.Column<string>(nullable: true),
                    Url = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Profiles", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "BlogChannel",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGeneratedOnAdd", true),
                    Author = table.Column<string>(nullable: true),
                    CreatedAtUtc = table.Column<DateTime>(nullable: false, defaultValueSql: "current_timestamp(6)")
                        .Annotation("MySql:ValueGeneratedOnAdd", true),
                    Description = table.Column<string>(nullable: true),
                    ImageUrl = table.Column<string>(nullable: true),
                    PageViewsLastMonth = table.Column<int>(nullable: false),
                    PageViewsLastWeek = table.Column<int>(nullable: false),
                    ProfileId = table.Column<int>(nullable: true),
                    UpdatedAtUtc = table.Column<DateTime>(nullable: false, defaultValueSql: "current_timestamp(6)")
                        .Annotation("MySql:ValueGeneratedOnAddOrUpdate", true),
                    Url = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_BlogChannel", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Blog_Profile",
                        column: x => x.ProfileId,
                        principalTable: "Profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "InstagramChannel",
                columns: table => new
                {
                    Id = table.Column<string>(nullable: false)
                        .Annotation("MySql:ValueGeneratedOnAdd", true),
                    Bio = table.Column<string>(nullable: true),
                    CreatedAtUtc = table.Column<DateTime>(nullable: false, defaultValueSql: "current_timestamp(6)")
                        .Annotation("MySql:ValueGeneratedOnAdd", true),
                    FollowedBy = table.Column<int>(nullable: false),
                    FullName = table.Column<string>(nullable: true),
                    ImageUrl = table.Column<string>(nullable: true),
                    ProfileId = table.Column<int>(nullable: true),
                    UpdatedAtUtc = table.Column<DateTime>(nullable: false, defaultValueSql: "current_timestamp(6)")
                        .Annotation("MySql:ValueGeneratedOnAddOrUpdate", true),
                    Username = table.Column<string>(nullable: true),
                    Website = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_InstagramChannel", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Instagram_Profile",
                        column: x => x.ProfileId,
                        principalTable: "Profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

           
            migrationBuilder.CreateIndex(
                name: "IX_BlogChannel_ProfileId",
                table: "BlogChannel",
                column: "ProfileId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_InstagramChannel_ProfileId",
                table: "InstagramChannel",
                column: "ProfileId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Profiles_BlogId",
                table: "Profiles",
                column: "BlogId");

            migrationBuilder.CreateIndex(
                name: "IX_Profiles_InstagramId",
                table: "Profiles",
                column: "InstagramId");

            migrationBuilder.CreateIndex(
                name: "IX_ProfileCategories_ProfileId",
                table: "ProfileCategories",
                column: "ProfileId");

            migrationBuilder.AddForeignKey(
                name: "FK_Profiles_BlogChannel_BlogId",
                table: "Profiles",
                column: "BlogId",
                principalTable: "BlogChannel",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

            migrationBuilder.AddForeignKey(
                name: "FK_Profiles_InstagramChannel_InstagramId",
                table: "Profiles",
                column: "InstagramId",
                principalTable: "InstagramChannel",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Blog_Profile",
                table: "BlogChannel");

            migrationBuilder.DropForeignKey(
                name: "FK_Instagram_Profile",
                table: "InstagramChannel");

            migrationBuilder.DropTable(
                name: "ProfileCategories");

            migrationBuilder.DropTable(
                name: "Categories");

            migrationBuilder.DropTable(
                name: "Profiles");

            migrationBuilder.DropTable(
                name: "BlogChannel");

            migrationBuilder.DropTable(
                name: "InstagramChannel");
        }
    }
}

It works for a many-to-many relationship that I've set up for Profile, Category and ProfileCategory join table. That is - deleting a profile successfully removes the entries in ProfileCategory

@joacar joacar changed the title Cascade delete not working Cascade delete not working one-to-one Dec 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant