3

有一天,我现在进行了投资,以找到没有成功的解决方案。

我喜欢创建一个简单的多对多关系。

为此,我创建了两个模型:

一个 Player 类,我将拥有“Turnaments”导航属性来查看哪些 turnaments 访问了玩家。

public class Player
{
    [Key]
    public Int64 PlayerId { get; set; }

    public string FirstName { get; set; }

    [Required]
    public string Surename { get; set; }

    public virtual ICollection<Turnament> Turnaments { get; set; }
}

和一个 Turnament 类,我将在其中拥有“Players”导航属性,以查看哪些玩家是 Turnament 的一部分。

public class Turnament
{
    [Key]
    public int TurnamentId { get; set; }

    [Required]
    public string Name { get; set; }

    public DateTime? StartDate { get; set; }

    public DateTime? EndDate { get; set; }

    public bool IsClosed { get; set; }

    public virtual ICollection<Player> Players { get; set; }
}

如果在 VS2012 的“包管理器控制台”窗口中通过命令“upate-database”(自动迁移 = on)开始迁移 - 我得到以下异常:

在表“PlayerTurnaments”上引入 FOREIGN KEY 约束“FK_dbo.PlayerTurnaments_dbo.Turnaments_Turnament_TurnamentId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。无法创建约束。请参阅以前的错误。

我也了解该异常的背景。

所以我会告诉实体框架,如果我被删除:

a) 玩家 EF 不应将级联删除推送到关联的 Turnament

b) Turnament EF 应该将级联删除推送到相关玩家

有了那个背景,循环路径就消失了。

所以我尝试通过Entity Framwork的fluent-API实现一个'Switch cascade delete off':

public class DataContext : DbContext
{
    public DbSet<Turnament> Turnaments { get; set; }
    public DbSet<Player> Players { get; set; }

    protected override void OnModelCreating( DbModelBuilder modelBuilder )
    {

        //modelBuilder.Entity<Turnament>()
        //    .HasMany( b => b.Players )
        //    .WithMany( a => a.Turnaments )
        //    .Map( m => m.MapLeftKey( "PlayerPid" )
        //                  .MapRightKey( "TurnamentPid" )
        //                  .ToTable( "TurnamentsJoinPlayers" ) );


        //modelBuilder.Entity<Player>().HasRequired( t => t.Turnaments ).
        //    WithMany().WillCascadeOnDelete( false );

        //modelBuilder.Entity<Turnament>().HasRequired( t => t.Players ).
        //    WithMany().WillCascadeOnDelete( false );

        //modelBuilder.Entity<Player>().HasRequired( t => t.TurnamentsRelation ).
        //    WithMany().HasForeignKey( p => p.PlayerId ).WillCascadeOnDelete( false );

        //modelBuilder.Entity<Player>().HasMany(p => p.TurnamentsRelation).

        base.OnModelCreating( modelBuilder );

    }
}

但是,正如您所见,一切都得到了赞扬,因为它没有帮助。

感谢所有有能力的帮助,因为只有一些只有破解才能理解的代码片段:) PS:我确信这种多对多关系的EF5(最终)有解决方案

4

1 回答 1

2

解决方案

public class MyAppContext : DbContext
{

   public MyAppContext()
   {
    // Somtimes helps to uncommend that line, then the Database will recreated!
    // (For people that are not fit enough in EF CodeFirst - like me ;)
    // 1) Uncommend 
    // 2) Start Application go on a site that access your data wait for
    //    Exception (No Database!)
    // 3) Unkommend line 
    // 4) Execute 'update-database' at the 'Package Manager Console' of VS 2012
    // Database.SetInitializer(new DropCreateDatabaseAlways<LivescoreContext>());
   }

   protected override void OnModelCreating( DbModelBuilder modelBuilder )
   {
        /* Players <-> Turnaments */
        modelBuilder.Entity<Player>()
            .HasMany( p => p.Turnaments )
            .WithMany( p => p.Players )
            .Map( mc =>
                {
                    mc.MapLeftKey( "PlayerPid" );
                    mc.MapRightKey( "TurnamentPid" );
                    mc.ToTable( "PlayersJoinTurnaments" );
                } );

        base.OnModelCreating( modelBuilder );
   }
}
于 2013-02-16T14:20:56.403 回答