【发布时间】:2017-05-07 19:44:27
【问题描述】:
我在两个实体之间具有多对多关系。一切正常。有没有办法在中间表(StoresPushNotifications)中定义 FK 的名称?
问的原因是mysql不允许定义长约束名。它会生成随机 FK 以防万一。当我尝试将迁移设置为较早的步骤时,它会中断迁移。
[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; }
}
在我的上下文文件中,
modelBuilder.Entity<StoreEntity>()
.HasMany<PushNotificationEntity>(s => s.PushNotifications)
.WithMany(c => c.Stores)
.Map(cs =>
{
cs.MapLeftKey("StoreId");
cs.MapRightKey("PushNotificationId");
cs.ToTable("StoresPushNotifications");
});
【问题讨论】:
标签: c# mysql entity-framework ef-code-first code-first