entity-framework - 如何摆脱 "An entity object cannot be referenced by multiple instances of IEntityChangeTracker"?

标签 entity-framework

我在 Ado.Net EF 中有一个模型。
我有一对多的关系,当我想添加实体时出现错误

“一个实体对象不能被 IEntityChangeTracker 的多个实例引用”

有什么线索吗?

类似的东西

Template template = new Template();
...
...
while (from < to)
{
    Course course = new Course();
    .....
    template.Course.Add(course);
    .....
}
courseEntities.AddToTemplate(template); // Problem line
courseEntities.SaveChanges();

最佳答案

在我开始在 HttpContext.Items 中存储数据上下文之前,我一直收到这条消息。属性(property)。这意味着您可以对当前 Web 请求使用相同的数据上下文。这样你就不会得到 2 个引用相同实体的数据上下文。

这是 DataContext Life Management 上的好帖子.

我希望它有帮助。

戴夫

关于entity-framework - 如何摆脱 "An entity object cannot be referenced by multiple instances of IEntityChangeTracker"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/620084/

相关文章:

c# - 从没有代理类的数据库加载?

c# - 将多个 Include() 与嵌套 Select() 结合使用时出现 EntityCommandExecutionException

.net - 在.Net 3.5应用程序中使用Entity Framework 4.0

c# - 使用 LINQ 查询语法 EF Core C# 的左外部联接

asp.net - web.config 中与 targetFramework 相关的配置错误

c# - 如何使用 Entity Framework 保存和加载 System.IO.Stream 对象?

entity-framework - Entity Framework - 自定义异常的 DbUpdateException

c# - GroupBy 返回不同的结果

c# - 使用 AutoMapper 从数据库加载实体?

sql - 如何通过 Entity Framework 启用 SQL 应用程序角色