我在Entity Framework Core 1.1中有以下实体:
public class Question {
public Int32 Id { get; set; }
public Int32 AuthorId { get; set; }
public String Content { get; set; }
public virtual User Author { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
}
public class Answer {
public Int32 Id { get; set; }
public Int32 QuestionId { get; set; }
public Int32 AuthorId { get; set; }
public String Content { get; set; }
public virtual Question Question { get; set; }
public virtual User Author { get; set; }
}
public class User : IdentityUser<Int32> {
public virtual ICollection<Answer> Answers { get; set; } = new List<Answer>();
public virtual ICollection<Question> Questions { get; set; } = new List<Question>();
}
public class Context : IdentityDbContext<User, Role, Int32> {
public DbSet<Answer> Answers { get; set; }
public DbSet<Question> Questions { get; set; }
public Context(DbContextOptions<Context> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder) {
base.OnModelCreating(builder);
builder.Entity<Question>(b =>
b.ToTable("Questions");
b.HasKey(x => x.Id);
b.Property(x => x.Content).HasMaxLength(400).IsRequired(true);
b.HasOne(x => x.Author).WithMany(x => x.Questions).HasForeignKey(x => x.AuthorId).IsRequired(true).OnDelete(DeleteBehavior.Restrict);
});
builder.Entity<Answer>(b =>
b.ToTable("Answers");
b.HasKey(x => x.Id);
b.Property(x => x.Content).HasMaxLength(4000).IsRequired(true);
b.HasOne(x => x.Author).WithMany(x => x.Answers).HasForeignKey(x => x.AuthorId).IsRequired(true).OnDelete(DeleteBehavior.Restrict);
b.HasOne(x => x.Question).WithMany(x => x.Answers).HasForeignKey(x => x.AuthorId).IsRequired(true).OnDelete(DeleteBehavior.Cascade);
});
}
}
我尝试创建一个问题列表(在下面的代码中我只添加一个):
List<Question> questions = new List<Question>();
questions.Add(new Question {
Approved = DateTime.UtcNow,
Author = context.Users.First(),
Content = "question",
Created = DateTime.UtcNow,
IsApproved = true,
Level = context.Levels.First(),
Updated = DateTime.UtcNow,
Answers = new List<Answer> {
new Answer {
Approved = DateTime.UtcNow,
Author = context.Users.Last(),
Content = "answer",
Created = DateTime.UtcNow,
IsApproved = true,
Updated = DateTime.UtcNow
}
}
});
context.AddRange(questions);
await context.SaveChangesAsync();
添加问题时,我收到以下错误:
System.AggregateException occurred
InvalidOperationException: Collection was modified; enumeration operation may not execute.
这是在EntityFramework 1.0.1上工作。迁移到1.1时出现错误。
如果我在Answer中注释代码行Author = context.Users.Last(),
,那么它可以正常工作。
我错过了什么吗?
答案 0 :(得分:0)
通常在保存时将整个对象附加为导航属性不是一个好主意,因为它可能会插入新记录。我的猜测是它在创建问题时尝试添加用户,这可能导致用户列表处于集合修改状态。
最好的方法是在创建对象之前先获得第一个和最后一个用户,然后再创建对象。
var firstUser = context.Users.First();
var lastUser = context.Users.Last();
List<Question> questions = new List<Question>();
questions.Add(new Question {
Approved = DateTime.UtcNow,
Author = firstUser,
Content = "question",
Created = DateTime.UtcNow,
IsApproved = true,
Level = context.Levels.First(),
Updated = DateTime.UtcNow,
Answers = new List<Answer> {
new Answer {
Approved = DateTime.UtcNow,
Author = lastUser,
Content = "answer",
Created = DateTime.UtcNow,
IsApproved = true,
Updated = DateTime.UtcNow
}
}
});
context.AddRange(questions);
await context.SaveChangesAsync();