我正在尝试在我的数据库中链接两个模型(“EventComments”和“ApplicationUsers”),但我一直收到此错误
错误号码:1785,州:0,班级:16 引入FOREIGN KEY约束 表'EventComments'上的'FK_EventComments_AspNetUsers_UserId'可以 导致循环或多个级联路径。指定ON DELETE NO ACTION或 ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。可以 不创建约束或索引。查看以前的错误。
以下是我的模特。
public class EventComment
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(2000)]
public string Message { get; set; }
[Required]
public int EventId { get; set; }
public Event Event { get; set; }
[Required]
public string UserId { get; set; }
public ApplicationUser User { get; set; }
}
public class ApplicationUser : IdentityUser
{
public ICollection<Event> Events { get; set; } = new HashSet<Event>();
public ICollection<EventComment> EventComments { get; set; } = new HashSet<EventComment>();
}
这是我的Fluent API配置
protected override void OnModelCreating(ModelBuilder builder)
{
//user
builder
.Entity<ApplicationUser>()
.HasMany(u => u.Events)
.WithOne(b => b.User);
builder
.Entity<ApplicationUser>()
.HasMany(u => u.EventComments)
.WithOne(b => b.User);
builder
.Entity<Event>()
.HasMany(e => e.Comments)
.WithOne(c => c.Event);
}
指定“删除”操作无论如何都无济于事。任何建议将不胜感激。