简体   繁体   English

实体框架多个级联路径错误

[英]entity framework multiple cascade paths error

What I've done so far to achieve what I want using Entity Framework is something like this: 到目前为止,我为使用实体框架实现所需的功能是这样的:

// User.cs
public class User {
  public Guid ID { get; set; } // column: user_id
  public virtual ICollection<Event> Events { get; set; }
}
// Event.cs
public class Event {
  public Guid ID { get; set; } // column: event_id
  public virtual Guid UserID { get; set; } // column: event_userid
  public virtual ICollection<User> Guests { get; set; }
}
// MyAppContext.cs
...
protected override void OnModelCreating(DbModelBuilder mb) {
  mb.Entity<User>()
    .HasKey(u => u.ID)
    .HasMany(u => u.Events)
    .WithOptional()
    .HasForeignKey(e => e.UserID);

  mb.Entity<Event>()
    .HasKey(e => e.ID)
    .HasMany(e => e.Guests)
    .WithMany();
}
...

I was expecting the database structure to be as follows: 我期望数据库结构如下:

TABLE: user
user_id uniqueidentifier not null primary key

TABLE: event
event_id uniqueidentifier not null primary key
event_userid uniqueidentifier not null foreign key references user(user_id)

TABLE: event_guests
event_id uniqueidentifier not null
user_id uniqueidentifier not null

I have a feeling that the fluent API I'm using above is not going to give the expected database structure and also, I get the following exception that I've no clue how to fix: 我有一种感觉,我在上面使用的流畅的API不会提供预期的数据库结构,而且,我得到了以下例外,我不知道如何解决:

Introducing FOREIGN KEY constraint 'FK_xxx' on table 'event_guests'
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. See previous errors.

I'm new to entity framework, any help would be much appreciated. 我是实体框架的新手,任何帮助将不胜感激。

Try replacing your configurations with a single many to many configuration. 尝试用一个多对多配置替换您的配置。

modelBuilder.Entity<User>()
            .HasMany(a => a.Events)
            .WithMany(b=> b.Guests)
            .Map(x =>
            {
                x.MapLeftKey("UserId");
                x.MapRightKey("EventId");
                x.ToTable("EventGuests");
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多个级联路径错误实体框架代码优先 - Multiple cascade paths error entity framework Code first 如何删除实体框架中的多个级联路径 - How to remove multiple cascade paths in entity framework 实体框架级联删除-循环或多个级联路径 - Entity Framework cascade delete - cycles or multiple cascade paths SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core 首先使用实体​​框架代码:循环或多个级联路径 - Entity Framework Code first: cycles or multiple cascade paths Entity Framework Core:可能导致循环或多个级联路径 - Entity Framework Core: may cause cycles or multiple cascade paths .Net实体框架循环级联路径 - .Net Entity Framework Cyclic Cascade Paths 多个级联路径错误-实体框架核心 - Multiple cascading paths Error - Entity Framework Core MVC 4实体框架同一表的两个外键导致循环或多个级联路径 - MVC 4 Entity Framework Two Foreign Keys to the Same Table Cause cycles or multiple cascade paths SQL多个级联路径/无法首先确定类型实体框架代码之间的关联的主体端 - Sql multiple cascade paths/ Unable to determine the principal end of an association between the types entity framework code first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM