.net 6 to .net 8, several foreigns keys will be removed and recreate in a migration
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 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; }
}
I suggest to take a look into the history of the other migrations to see if there's some particular behaviour.
It's ok, I made some changes and apply the migration without error, it was due to upgrade EF6 to EF8 (EF6 to EF7)