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

Add migrations drop and create foregin key on table that not is modified #3751

Closed
Tasteful opened this issue Nov 13, 2015 · 6 comments
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@Tasteful
Copy link
Contributor

When adding a new migraion a lot of DropForeginKey and AddForeginKey are added even if the table not is touched by the rest of the migration commands. See example the CategoryFieldData table in the below migration.

Is there any reason for this approach?

Using beta8-version.

using System;
using System.Collections.Generic;
using Microsoft.Data.Entity.Migrations;

namespace Application.Migrations
{
    public partial class WorkflowFieldFramework : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(name: "FK_Products_AssortmentFieldData_Assortment_OwnerId", schema: "Products", table: "AssortmentFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_CategoryFieldData_Category_OwnerId", schema: "Products", table: "CategoryFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_FieldDefinitionFieldData_FieldDefinition_OwnerId", schema: "Products", table: "FieldDefinitionFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_FieldTemplateFieldData_FieldTemplate_OwnerId", schema: "Products", table: "FieldTemplateFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_ProductFieldData_Product_OwnerId", schema: "Products", table: "ProductFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_VariantFieldData_Variant_OwnerId", schema: "Products", table: "VariantFieldData");
            migrationBuilder.DropForeignKey(name: "FK_WorkflowTaskConditionEntity_WorkflowTaskEntity_WorkflowTaskId", schema: "Products", table: "WorkflowTaskCondition");
            migrationBuilder.DropForeignKey(name: "FK_WorkflowTaskEntity_WorkflowEntity_WorkflowId", schema: "Products", table: "WorkflowTask");
            migrationBuilder.DropTable(name: "WorkflowLocalization", schema: "Products");
            migrationBuilder.DropTable(name: "WorkflowTaskLocalization", schema: "Products");
            migrationBuilder.CreateTable(
                name: "WorkflowFieldData",
                schema: "Products",
                columns: table => new
                {
                    OwnerId = table.Column<Guid>(nullable: false),
                    Id = table.Column<string>(nullable: false),
                    Culture = table.Column<string>(nullable: false),
                    BooleanValue = table.Column<bool>(nullable: true),
                    DateTimeValue = table.Column<DateTime>(nullable: true),
                    DecimalValue = table.Column<decimal>(nullable: true),
                    GuidValue = table.Column<Guid>(nullable: true),
                    IndexedTextValue = table.Column<string>(nullable: true),
                    IntValue = table.Column<long>(nullable: true),
                    JsonData = table.Column<string>(nullable: true),
                    TextValue = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Products_WorkflowFieldData", x => new { x.OwnerId, x.Id, x.Culture });
                    table.ForeignKey(
                        name: "FK_Products_WorkflowFieldData_Workflow_OwnerId",
                        column: x => x.OwnerId,
                        principalSchema: "Products",
                        principalTable: "Workflow",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });
            migrationBuilder.CreateTable(
                name: "WorkflowTaskFieldData",
                schema: "Products",
                columns: table => new
                {
                    OwnerId = table.Column<Guid>(nullable: false),
                    Id = table.Column<string>(nullable: false),
                    Culture = table.Column<string>(nullable: false),
                    BooleanValue = table.Column<bool>(nullable: true),
                    DateTimeValue = table.Column<DateTime>(nullable: true),
                    DecimalValue = table.Column<decimal>(nullable: true),
                    GuidValue = table.Column<Guid>(nullable: true),
                    IndexedTextValue = table.Column<string>(nullable: true),
                    IntValue = table.Column<long>(nullable: true),
                    JsonData = table.Column<string>(nullable: true),
                    TextValue = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Products_WorkflowTaskFieldData", x => new { x.OwnerId, x.Id, x.Culture });
                    table.ForeignKey(
                        name: "FK_Products_WorkflowTaskFieldData_WorkflowTask_OwnerId",
                        column: x => x.OwnerId,
                        principalSchema: "Products",
                        principalTable: "WorkflowTask",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });
            migrationBuilder.CreateIndex(
                name: "IX_Products_WorkflowFieldData_IndexedTextValue",
                schema: "Products",
                table: "WorkflowFieldData",
                column: "IndexedTextValue");
            migrationBuilder.CreateIndex(
                name: "IX_Products_WorkflowFieldData_OwnerId",
                schema: "Products",
                table: "WorkflowFieldData",
                column: "OwnerId");
            migrationBuilder.CreateIndex(
                name: "IX_Products_WorkflowTaskFieldData_IndexedTextValue",
                schema: "Products",
                table: "WorkflowTaskFieldData",
                column: "IndexedTextValue");
            migrationBuilder.CreateIndex(
                name: "IX_Products_WorkflowTaskFieldData_OwnerId",
                schema: "Products",
                table: "WorkflowTaskFieldData",
                column: "OwnerId");
            migrationBuilder.AddForeignKey(
                name: "FK_Products_AssortmentFieldData_Assortment_OwnerId",
                schema: "Products",
                table: "AssortmentFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "Assortment",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_Products_CategoryFieldData_Category_OwnerId",
                schema: "Products",
                table: "CategoryFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "Category",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_Products_FieldDefinitionFieldData_FieldDefinition_OwnerId",
                schema: "Products",
                table: "FieldDefinitionFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "FieldDefinition",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_Products_FieldTemplateFieldData_FieldTemplate_OwnerId",
                schema: "Products",
                table: "FieldTemplateFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "FieldTemplate",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_Products_ProductFieldData_Product_OwnerId",
                schema: "Products",
                table: "ProductFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "Product",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_Products_VariantFieldData_Variant_OwnerId",
                schema: "Products",
                table: "VariantFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "Variant",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_WorkflowTaskConditionEntity_WorkflowTaskEntity_WorkflowTaskId",
                schema: "Products",
                table: "WorkflowTaskCondition",
                column: "WorkflowTaskId",
                principalSchema: "Products",
                principalTable: "WorkflowTask",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
            migrationBuilder.AddForeignKey(
                name: "FK_WorkflowTaskEntity_WorkflowEntity_WorkflowId",
                schema: "Products",
                table: "WorkflowTask",
                column: "WorkflowId",
                principalSchema: "Products",
                principalTable: "Workflow",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(name: "FK_Products_AssortmentFieldData_Assortment_OwnerId", schema: "Products", table: "AssortmentFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_CategoryFieldData_Category_OwnerId", schema: "Products", table: "CategoryFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_FieldDefinitionFieldData_FieldDefinition_OwnerId", schema: "Products", table: "FieldDefinitionFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_FieldTemplateFieldData_FieldTemplate_OwnerId", schema: "Products", table: "FieldTemplateFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_ProductFieldData_Product_OwnerId", schema: "Products", table: "ProductFieldData");
            migrationBuilder.DropForeignKey(name: "FK_Products_VariantFieldData_Variant_OwnerId", schema: "Products", table: "VariantFieldData");
            migrationBuilder.DropForeignKey(name: "FK_WorkflowTaskConditionEntity_WorkflowTaskEntity_WorkflowTaskId", schema: "Products", table: "WorkflowTaskCondition");
            migrationBuilder.DropForeignKey(name: "FK_WorkflowTaskEntity_WorkflowEntity_WorkflowId", schema: "Products", table: "WorkflowTask");
            migrationBuilder.DropTable(name: "WorkflowFieldData", schema: "Products");
            migrationBuilder.DropTable(name: "WorkflowTaskFieldData", schema: "Products");
            migrationBuilder.CreateTable(
                name: "WorkflowLocalization",
                schema: "Products",
                columns: table => new
                {
                    Culture = table.Column<string>(nullable: false),
                    WorkflowId = table.Column<Guid>(nullable: false),
                    Name = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WorkflowLocalizationEntity", x => new { x.Culture, x.WorkflowId });
                    table.ForeignKey(
                        name: "FK_WorkflowLocalizationEntity_WorkflowEntity_WorkflowId",
                        column: x => x.WorkflowId,
                        principalSchema: "Products",
                        principalTable: "Workflow",
                        principalColumn: "Id");
                });
            migrationBuilder.CreateTable(
                name: "WorkflowTaskLocalization",
                schema: "Products",
                columns: table => new
                {
                    Culture = table.Column<string>(nullable: false),
                    WorkflowTaskId = table.Column<Guid>(nullable: false),
                    Name = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WorkflowTaskLocalizationEntity", x => new { x.Culture, x.WorkflowTaskId });
                    table.ForeignKey(
                        name: "FK_WorkflowTaskLocalizationEntity_WorkflowTaskEntity_WorkflowTaskId",
                        column: x => x.WorkflowTaskId,
                        principalSchema: "Products",
                        principalTable: "WorkflowTask",
                        principalColumn: "Id");
                });
            migrationBuilder.CreateIndex(
                name: "IX_WorkflowLocalizationEntity_WorkflowId",
                schema: "Products",
                table: "WorkflowLocalization",
                column: "WorkflowId");
            migrationBuilder.CreateIndex(
                name: "IX_WorkflowTaskLocalizationEntity_WorkflowTaskId",
                schema: "Products",
                table: "WorkflowTaskLocalization",
                column: "WorkflowTaskId");
            migrationBuilder.AddForeignKey(
                name: "FK_Products_AssortmentFieldData_Assortment_OwnerId",
                schema: "Products",
                table: "AssortmentFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "Assortment",
                principalColumn: "Id");
            migrationBuilder.AddForeignKey(
                name: "FK_Products_CategoryFieldData_Category_OwnerId",
                schema: "Products",
                table: "CategoryFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "Category",
                principalColumn: "Id");
            migrationBuilder.AddForeignKey(
                name: "FK_Products_FieldDefinitionFieldData_FieldDefinition_OwnerId",
                schema: "Products",
                table: "FieldDefinitionFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "FieldDefinition",
                principalColumn: "Id");
            migrationBuilder.AddForeignKey(
                name: "FK_Products_FieldTemplateFieldData_FieldTemplate_OwnerId",
                schema: "Products",
                table: "FieldTemplateFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "FieldTemplate",
                principalColumn: "Id");
            migrationBuilder.AddForeignKey(
                name: "FK_Products_ProductFieldData_Product_OwnerId",
                schema: "Products",
                table: "ProductFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "Product",
                principalColumn: "Id");
            migrationBuilder.AddForeignKey(
                name: "FK_Products_VariantFieldData_Variant_OwnerId",
                schema: "Products",
                table: "VariantFieldData",
                column: "OwnerId",
                principalSchema: "Products",
                principalTable: "Variant",
                principalColumn: "Id");
            migrationBuilder.AddForeignKey(
                name: "FK_WorkflowTaskConditionEntity_WorkflowTaskEntity_WorkflowTaskId",
                schema: "Products",
                table: "WorkflowTaskCondition",
                column: "WorkflowTaskId",
                principalSchema: "Products",
                principalTable: "WorkflowTask",
                principalColumn: "Id");
            migrationBuilder.AddForeignKey(
                name: "FK_WorkflowTaskEntity_WorkflowEntity_WorkflowId",
                schema: "Products",
                table: "WorkflowTask",
                column: "WorkflowId",
                principalSchema: "Products",
                principalTable: "Workflow",
                principalColumn: "Id");
        }
    }
}
@Tasteful
Copy link
Contributor Author

Maybe I can answer it this by myself :) I was adding a navigation property on the base-class for the entities that have tables that ends with "FieldData". But that navigation property was anyway not modifying the database schema.

@smitpatel
Copy link
Contributor

I ran diff of both Up & Down methods.
Ignoring the 2 tables which are deleted and updated and focusing on AddForeignKey operations, the up method is setting onDelete: ReferentialAction.Cascade.
Did you make any changes on null ability of fk properties? Or updated packages recently?

@Tasteful
Copy link
Contributor Author

The cascade delete was on the table before. When inspected the ApplicationDbContextModelSnapshot both before and after I created the new migration I can see that the relation is there but missing the .WillCascadeOnDelete() on the relation.

            modelBuilder.Entity("Application.Core.Products.CategoryEntity+FieldDataEntity", b =>
                {
                    b.HasOne("Application.Core.Products.CategoryEntity")
                        .WithMany()
                        .ForeignKey("OwnerId")
                        .Annotation("Relational:Name", "FK_Products_CategoryFieldData_Category_OwnerId");
                });

@bricelam
Copy link
Contributor

Looks like the model snapshot generator needs to be updated to capture the WillCascadeOnDelete configuration.

@Tasteful
Copy link
Contributor Author

Tested in rc1-16305 and the model snappshot generator is still missing the WillCascadeOnDelete configuration.

@smitpatel
Copy link
Contributor

Will send out PR for fix soon. 😄

@smitpatel smitpatel added this to the 7.0.0-rc2 milestone Nov 16, 2015
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants