I have done this before, but for some reason cannot get it to work in EF5.
我以前做过这个,但由于某种原因无法让它在EF5中运行。
Usually it just automatically picks up when I have many to many relationships like this one...
通常它只是在我有很多这样的关系时才会自动捡起......
public class Beer
{
public int Id { get; set; }
public virtual ICollection<Restaurant> Restaurants { get; set; }
}
public class Restaurant
{
public int Id { get; set; }
public virtual ICollection<Beer> Beers { get; set; }
}
I am wanting a RestaurantsBeers table or whatever with just RestaurantId and BeerId.
我想要一个只有RestaurantId和BeerId的RestaurantsBeers餐桌。
When I create it using the normal Code First way by just running the application it works.
当我使用正常的Code First方式创建它时,只需运行应用程序即可。
Using migrations though, it won't create that table.
但是,使用迁移,它不会创建该表。
I ran Enable-Migrations
then Add-Migration FirstDb
and finally Update-Database
... No dice...
我运行Enable-Migrations然后添加迁移FirstDb,最后运行Update-Database ...没有骰子......
Also tried this...
还试过这个......
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Beer>()
.HasMany(b => b.Restaurants)
.WithMany(a => a.Beers)
.Map(m => m.MapLeftKey("BeerId")
.MapRightKey("RestaurantId")
.ToTable("BeersRestaurants"));
}
1 个解决方案
#1
3
Migrations to create a new M2M relationship are not supported yet in EF5.0RC per my experience trying to track down the same issue. Thus why it will work on standard DB creation but doesn't work with Migration features. You can export the create SQL from the standard code first database initialization and run it manually on the migration for now.
根据我尝试追踪同一问题的经验,EF5.0RC尚不支持迁移以创建新的M2M关系。这就是为什么它将用于标准数据库创建但不适用于迁移功能。您可以从标准代码首次数据库初始化导出create SQL,并立即在迁移时手动运行它。
This should be resolved when EF5.0 goes RTM but for now we have to wait it out.
这应该在EF5.0进入RTM时解决,但现在我们必须等待它。
#1
3
Migrations to create a new M2M relationship are not supported yet in EF5.0RC per my experience trying to track down the same issue. Thus why it will work on standard DB creation but doesn't work with Migration features. You can export the create SQL from the standard code first database initialization and run it manually on the migration for now.
根据我尝试追踪同一问题的经验,EF5.0RC尚不支持迁移以创建新的M2M关系。这就是为什么它将用于标准数据库创建但不适用于迁移功能。您可以从标准代码首次数据库初始化导出create SQL,并立即在迁移时手动运行它。
This should be resolved when EF5.0 goes RTM but for now we have to wait it out.
这应该在EF5.0进入RTM时解决,但现在我们必须等待它。