efcore icon indicating copy to clipboard operation
efcore copied to clipboard

.net 6 to .net 8, several foreigns keys will be removed and recreate in a migration

Open Alexandre-NAUD opened this issue 2 years ago • 3 comments

Hi,

After updating our project to .net 8.0 and perform a migration with no model change, severals foreigns keys are dropped and recreated with Cascade onDelete like this :

migrationBuilder.DropForeignKey(
    name: "FK_Common_UpdateConditionalStyleLock_Common_UpdateLock_UpdateLockID",
    table: "Common_UpdateConditionalStyleLock");

migrationBuilder.AddForeignKey(
    name: "FK_Common_UpdateConditionalStyleLock_Common_UpdateLock_UpdateLockID",
    table: "Common_UpdateConditionalStyleLock",
    column: "UpdateLockID",
    principalTable: "Common_UpdateLock",
    principalColumn: "UpdateLockID",
    onDelete: ReferentialAction.Cascade);

These keys represents an inheritance :

[Table("Common_UpdateLock")]
public class UpdateLock
{
    [Required]
    public long UpdateLockID { get; set; }

    [Required]
    public virtual User LockedBy { get; set; }

    [Column(TypeName = "DateTime")]
    public DateTime LockDate { get; set; }
}

[Table("Common_UpdateConditionalStyleLock")]
public class UpdateConditionalStyleLock : UpdateLock
{
    [Required]
    public int ConditionalStyleID { get; set; }
}

With .net 6.0 and EFCore 6, no problems.

Have you any idea to resolve this problem ?

Thank you

EF Core version: 8.0.1 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 8.0) Operating system: Windows 11 IDE: Visual Studio 2022 17.8.5

Alexandre-NAUD avatar Jan 30 '24 15:01 Alexandre-NAUD

@Alexandre-NAUD I am not able to reproduce this--see my code below. Note that the relationship given only the code posted above is a cascade relationship, both in EF Core 6 and 8.

using (var context = new SomeDbContext())
{
    await context.Database.EnsureDeletedAsync();
    // await context.Database.EnsureCreatedAsync();
    //await context.SaveChangesAsync();
}

public class SomeDbContext : DbContext
{
	public DbSet<UpdateLock> UpdateLocks { get; set; }
	
	protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		=> optionsBuilder
			.UseSqlServer(@"Data Source=localhost;Database=One;Integrated Security=True;Trust Server Certificate=True")
			.LogTo(Console.WriteLine, LogLevel.Information)
			.EnableSensitiveDataLogging();
}

[Table("Common_UpdateLock")]
public class UpdateLock
{
	[Required]
	public long UpdateLockID { get; set; }

	[Required]
	public virtual User LockedBy { get; set; }

	[Column(TypeName = "DateTime")]
	public DateTime LockDate { get; set; }
}

public class User
{
	[Required]
	public long UserID { get; set; }
}

[Table("Common_UpdateConditionalStyleLock")]
public class UpdateConditionalStyleLock : UpdateLock
{
	[Required]
	public int ConditionalStyleID { get; set; }
}

ajcvickers avatar Jan 31 '24 12:01 ajcvickers

I suggest to take a look into the history of the other migrations to see if there's some particular behaviour.

Ryujose avatar Feb 02 '24 13:02 Ryujose

It's ok, I made some changes and apply the migration without error, it was due to upgrade EF6 to EF8 (EF6 to EF7)

Alexandre-NAUD avatar Feb 02 '24 14:02 Alexandre-NAUD