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

BUG? Created migration throw exception and cannot be reversed (only delete) #5966

Closed
Bartmax opened this issue Jul 4, 2016 · 1 comment
Closed
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

@Bartmax
Copy link

Bartmax commented Jul 4, 2016

Steps to reproduce

Using this model:

public class ImageFile 
{
    public Guid ImageFileId { get; private set; }

    [Required]
    public string Filename { get; private set; }

    public string OriginalFilename { get; private set; }

    public string ContentType { get; private set; }

    public string Folder { get; set; }
}

public class Company
{
    public int CompanyId { get; set; }
    public ImageFile Watermark { get; set; }
    public ImageFile Logo { get; set; }
    public ICollection<Screenshot> Screenshots { get; set; }
    public ICollection<Photos> Photos { get; set; }
}

public abstract class ImageOrder
{
    public int ImageOrderId { get; set; }
    public int Order { get; set; }
    public ImageFile ImageFile { get; set; }
}

public class Photos : ImageOrder
{
    public string Caption { get; set; }
}

public class Screenshot : ImageOrder
{
    public string Other { get; set; }
}
### The issue 1. Migration code generated has duplicate data. 2. `ef database update` throws `Sequence contains more than one matching element` 3. `ef migrations remove` gives no error but migration is **not removed**

Migration code

public partial class initial : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "ImageFile",
                columns: table => new
                {
                    ImageFileId = table.Column<Guid>(nullable: false),
                    ContentType = table.Column<string>(nullable: true),
                    Filename = table.Column<string>(nullable: false),
                    Folder = table.Column<string>(nullable: true),
                    OriginalFilename = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ImageFile", x => x.ImageFileId);
                });

            migrationBuilder.CreateTable(
                name: "Companies",
                columns: table => new
                {
                    CompanyId = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    LogoImageFileId = table.Column<Guid>(nullable: true),
                    WatermarkImageFileId = table.Column<Guid>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Companies", x => x.CompanyId);
                    table.ForeignKey(
                        name: "FK_Companies_ImageFile_LogoImageFileId",
                        column: x => x.LogoImageFileId,
                        principalTable: "ImageFile",
                        principalColumn: "ImageFileId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_Companies_ImageFile_WatermarkImageFileId",
                        column: x => x.WatermarkImageFileId,
                        principalTable: "ImageFile",
                        principalColumn: "ImageFileId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "ImageOrder",
                columns: table => new
                {
                    ImageOrderId = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    Discriminator = table.Column<string>(nullable: false),
                    ImageFileId = table.Column<Guid>(nullable: true),
                    Order = table.Column<int>(nullable: false),
                    Caption = table.Column<string>(nullable: true),
                    CompanyId = table.Column<int>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ImageOrder", x => x.ImageOrderId);
                    table.ForeignKey(
                        name: "FK_ImageOrder_ImageFile_ImageFileId",
                        column: x => x.ImageFileId,
                        principalTable: "ImageFile",
                        principalColumn: "ImageFileId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ImageOrder_Companies_CompanyId",
                        column: x => x.CompanyId,
                        principalTable: "Companies",
                        principalColumn: "CompanyId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ImageOrder_Companies_CompanyId",
                        column: x => x.CompanyId,
                        principalTable: "Companies",
                        principalColumn: "CompanyId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Companies_LogoImageFileId",
                table: "Companies",
                column: "LogoImageFileId");

            migrationBuilder.CreateIndex(
                name: "IX_Companies_WatermarkImageFileId",
                table: "Companies",
                column: "WatermarkImageFileId");

            migrationBuilder.CreateIndex(
                name: "IX_ImageOrder_ImageFileId",
                table: "ImageOrder",
                column: "ImageFileId");

            migrationBuilder.CreateIndex(
                name: "IX_ImageOrder_CompanyId",
                table: "ImageOrder",
                column: "CompanyId");
        }

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

            migrationBuilder.DropTable(
                name: "Companies");

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

Stack trace

System.InvalidOperationException: Sequence contains more than one matching element
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.GetColumnType(String schema, String table, String name, Type clrType, Nullable`1 unicode, Nullable`1 maxLength, Boolean rowVersion, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.ColumnDefinition(String schema, String table, String name, Type clrType, String type, Nullable`1 unicode, Nullable`1 maxLength, Boolean rowVersion, Boolean nullable, Object defaultValue, String defaultValueSql, String computedColumnSql, IAnnotatable annotatable, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.ColumnDefinition(String schema, String table, String name, Type clrType, String type, Nullable`1 unicode, Nullable`1 maxLength, Boolean rowVersion, Boolean nullable, Object defaultValue, String defaultValueSql, String computedColumnSql, Boolean identity, IAnnotatable annotatable, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.ColumnDefinition(String schema, String table, String name, Type clrType, String type, Nullable`1 unicode, Nullable`1 maxLength, Boolean rowVersion, Boolean nullable, Object defaultValue, String defaultValueSql, String computedColumnSql, IAnnotatable annotatable, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.ColumnDefinition(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(CreateTableOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
### Further technical details

EF Core version: 1.0.0
Operating system: Windows
Visual Studio version: VS 2015 Enterprise

Other details about my project setup:

I cannot find a way to create the following (3 tables) schema using code first.
I tried everything, it just looks impossible.

Images:
ImageId - FileName

Company:
CompanyId - Name - Logo_ImageId - Watermark_ImageId

CompanyImages:
CompanyId - Discriminator - ImageId - Order
@rowanmiller rowanmiller added this to the 1.1.0 milestone Jul 5, 2016
@bricelam
Copy link
Contributor

Fixed by @AndriySvyryd in #6143

@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 17, 2016
@bricelam bricelam assigned AndriySvyryd and unassigned bricelam Aug 17, 2016
@AndriySvyryd AndriySvyryd removed their assignment Aug 17, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.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

5 participants