efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Owned entity type use one type multiple times - add-migration will always find differences and therefore add the same migration again

Open Alexxtheonly opened this issue 8 years ago • 1 comments

Steps to reproduce

Use following classes: Model:

      public class TestModel
      {
        public int Id { get; set; }
       
        public CollectionComplexType<int> Values { get; set; }
    
        public CollectionComplexType<int> MoreValues { get; set; }
      }

ComplexType:

      public class CollectionComplexType<T> : Collection<T>
      {
        public string Serialized
        {
          get => JsonConvert.SerializeObject(this);
          private set
          {
            Clear();
    
            if (string.IsNullOrEmpty(value))
            {
              return;
            }
    
            foreach (var item in JsonConvert.DeserializeObject<T[]>(value))
            {
              Add(item);
            }
          }
        }
      }

Context:

      modelBuilder.Entity<TestModel>(e =>
      {
        e.HasKey(o => o.Id);
        e.OwnsOne(o => o.Values).Property(o => o.Serialized).HasColumnName(nameof(TestModel.Values));
        e.OwnsOne(o => o.MoreValues).Property(o => o.Serialized).HasColumnName(nameof(TestModel.MoreValues));
      });

Run add-migration SameMigration. Run add-migration SameMigration1, it will create the same migration over and over.

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "TestModels_MoreValues");

            migrationBuilder.AddColumn<string>(
                name: "MoreValues_Serialized",
                table: "TestModels",
                nullable: true);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "MoreValues_Serialized",
                table: "TestModels");

            migrationBuilder.CreateTable(
                name: "TestModels_MoreValues",
                columns: table => new
                {
                    TestModelId = table.Column<int>(nullable: false),
                    Serialized = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TestModels_MoreValues", x => x.TestModelId);
                    table.ForeignKey(
                        name: "FK_TestModels_MoreValues_TestModels_TestModelId",
                        column: x => x.TestModelId,
                        principalTable: "TestModels",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });
        }

Further technical details

EF Core version: 2.1.0-preview1-final Database Provider: Sqlite Operating system: Windows 10 IDE: Visual Studio 2017 15.6.3

Alexxtheonly avatar Mar 21 '18 09:03 Alexxtheonly

This appears to be fixed (probably by @AndriySvyryd). Having the owned type inherit from Collection<> caused an incorrect model to be built.

bricelam avatar Apr 03 '18 21:04 bricelam