Definition of FK names in many ways

I have a many-to-many relationship between two objects. Everything is working fine. Is there a way to determine the FK names in the staging table (StoresPushNotifications)?

The reason for the query is that mysql does not allow you to define long constraint names. It generates a random FK in the case. It aborts the migration when I try to set the transition to an earlier step.

[Table("Stores")]
public class StoreEntity
{
    [Key]
    public int Id { get; set; }

    public virtual ICollection<PushNotificationEntity> PushNotifications { get; set; }
}


[Table("PushNotifications")]
public class PushNotificationEntity
{
    [Key]
    public int Id { get; set; }
    public ICollection<StoreEntity> Stores { get; set; }
}

In my context file

modelBuilder.Entity<StoreEntity>()
    .HasMany<PushNotificationEntity>(s => s.PushNotifications)
    .WithMany(c => c.Stores)                
    .Map(cs =>
    {
        cs.MapLeftKey("StoreId");
        cs.MapRightKey("PushNotificationId");
        cs.ToTable("StoresPushNotifications");
    });
+4
source share
1 answer

I had a similar problem and it was actually related to the length of the transition key than the foreign keys.

I decided by changing the way migration was configured:

internal sealed class Configuration : DbMigrationsConfiguration<CCDatabase.CCDbContext>

public Configuration()
{
    AutomaticMigrationsEnabled = false;
    MigrationsDirectory = @"Migrations";

    SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());

    SetHistoryContextFactory("MySql.Data.MySqlClient", (conn, schema) => new MySqlHistoryContext(conn, schema));
}

public class MySqlHistoryContext : HistoryContext
{

    public MySqlHistoryContext(DbConnection connection, string defaultSchema) : base(connection, defaultSchema)
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(100).IsRequired();
        modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(200).IsRequired();
    }
}   
0

Source: https://habr.com/ru/post/1664530/


All Articles