未能首先使用Code设置ondelete Cascade

时间:2018-04-04 20:57:31

标签: c# entity-framework entity-framework-core

我有Client类和依赖的MatriculaCliente类,它们有一个(客户端)到很多关系,我需要ondelete级联删除客户端删除后的所有客户端matricula's,不知道为什么不工作这是怎么回事我试着去做

客户端

public class Cliente
{
    [Key]
    public string CONCLIENTE { get; set; }
    public string tipoDocumentoCliente { get; set; }
    public string nomeCliente { get; set; }          
    public string escolaridadeCliente { get; set; }

    public ICollection<MatriculaCliente> matriculas { get; set; }

    public Cliente() {
        matriculas = new Collection<MatriculaCliente>();
    }
}

Matricula

public class MatriculaCliente
{
    [Key]
    public string conmatriculacliente { get; set; }

    public string concliente { get; set; }
    public Cliente cliente { get; set; }
    public string tipoDocumento { get; set; }
    public string descricaoNegocio { get; set; }
    public string matriculaCliente { get; set; }
}

这是我的迁移历史,你可以看到ondelete是限制

    public partial class ModeloClienteMatricula : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Clientes",
                columns: table => new
                {
                    CONCLIENTE = table.Column<string>(nullable: false),
                    cepCliente = table.Column<string>(nullable: true),
                    cpfCliente = table.Column<string>(nullable: true),
                    dataDeclaracaoPepCliente = table.Column<DateTime>(nullable: false),
                    dataNascimentoCliente = table.Column<DateTime>(nullable: false),
                    escolaridadeCliente = table.Column<string>(nullable: true),
                    estadoCivilCliente = table.Column<string>(nullable: true),
                    nomeCliente = table.Column<string>(nullable: true),
                    rendaCliente = table.Column<int>(nullable: false),
                    sexoCliente = table.Column<string>(nullable: true),
                    tipoDocumentoCliente = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Clientes", x => x.CONCLIENTE);
                });

            migrationBuilder.CreateTable(
                name: "Matriculas",
                columns: table => new
                {
                    conmatriculacliente = table.Column<string>(nullable: false),
                    concliente = table.Column<string>(nullable: true),
                    descricaoNegocio = table.Column<string>(nullable: true),
                    matriculaCliente = table.Column<string>(nullable: true),
                    tipoDocumento = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Matriculas", x => x.conmatriculacliente);
                    table.ForeignKey(
                        name: "FK_Matriculas_Clientes_concliente",
                        column: x => x.concliente,
                        principalTable: "Clientes",
                        principalColumn: "CONCLIENTE",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Matriculas_concliente",
                table: "Matriculas",
                column: "concliente");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Matriculas");

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

1 个答案:

答案 0 :(得分:0)

您必须使用流畅的API来执行此操作。

尝试将以下内容添加到DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{   
    modelBuilder.Entity<Cliente>()
        .HasOptional(a => a.matriculas)
        .WithOptionalDependent()
        .WillCascadeOnDelete(true);
}