[英]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.