question

kire-2296 avatar image
0 Votes"
kire-2296 asked kire-2296 answered

EF migration owned object rename property strange migration actions

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");
         ...
       }
 }








dotnet-entity-framework-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

kire-2296 avatar image
0 Votes"
kire-2296 answered

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");










5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.