实体框架6 - 导致多个级联路径错误的多对多外键约束

时间:2017-10-26 17:47:58

标签: entity-framework ef-fluent-api cascading-deletes

我有多对多的关系定义如下:

public class Ticket 
{
     public int Id { get; set; }

     public ICollection<ApplicationUser> TicketSubscribers { get; set; }
}


public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
     public ICollection<Ticket> SubscriberTickets { get; set; }
}

流畅的api映射如下:

 public ApplicationUserConfiguration()
 {
       HasMany<Ticket>(u => u.SubscriberTickets)
       .WithMany(t => t.TicketSubscribers)
       .Map(
           ts =>
           {
               ts.MapLeftKey("UserId");
               ts.MapRightKey("TicketId");
               ts.ToTable("TicketSubscriber");
           });
 }

迁移如下:

CreateTable(
            "dbo.TicketSubscriber",
            c => new
                {
                    UserId = c.String(nullable: false, maxLength: 128),
                    TicketId = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.UserId, t.TicketId })
            .ForeignKey("dbo.User", t => t.UserId, cascadeDelete: true)
            .ForeignKey("dbo.Ticket", t => t.TicketId, cascadeDelete: true)
            .Index(t => t.UserId)
            .Index(t => t.TicketId);

当我运行更新数据库时,我收到以下错误:

Introducing FOREIGN KEY constraint 'FK_dbo.TicketSubscriber_dbo.Ticket_TicketId' on table 'TicketSubscriber' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.

我不知道如何在多对多映射中指定cascade delete false?

为什么会在多对多映射中生成此错误。我该如何解决这个问题?

0 个答案:

没有答案