EF migration owned object rename property strange migration actions

kire 1 Reputation point
2021-11-10T12:28:44.26+00:00

When I create a new migration file after I change one property of an owned type object, the migration will delete all the columns, rename a few columns and add all the columns.

I get an error if I run the migration file. (error like: cannot drop column, because it doesn`t exist).

This happen now in a big project with some entity with a lot of owned types. It`s a little bit difficult to show all the code.

Here is a example of an entity.

public class TestTemplate : BaseEntity
{
public bool isActive { get; set; }
public TestTemplateContainer template { get; set; } = new TestTemplateContainer();
}

[Owned]
public class TestTemplateContainer
{
public TTGeneral general { get; set; } = new TTGeneral();
....
}

[Owned]
public class TTGeneral
{
public string model { get; set; } = "";
....
}

For example ,if I rename the model property in TTGeneral to new model, I expect only a rename of a column in the migration file. That`s not happen now.

ContextFile:

only the normal entity(the owner) has DBSet property.
In the OnModelCreating I only use the normal entity to table.

public class RecipeContext : DbContext
{
public class RecipeContext : DbContext
{
public RecipeContext(DbContextOptions<RecipeContext> options) : base(options) {
this.Database.SetCommandTimeout(300);
}

     public DbSet<BasePreset> BasePreset { get; set; }


     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {             
        modelBuilder.Entity<BasePreset>().ToTable("Preset");
        ...
      }
}
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. kire 1 Reputation point
    2021-11-10T16:39:04.467+00:00

    Finally I found maybe a reason why it`s not working.

    I didn`t know that mssql server has a limit in the length of the column name. Name in the created migration ends with an ~ character.

            migrationBuilder.DropColumn(
                name: "assemblyParameters_station_station1_brakeRing2_PTOLineTemplateStation1PTOLineTemplateStationPTOLineTemplateContainerPTOLineTemp~",
                table: "Recipe");
    
    0 comments No comments