我是靠谱客的博主 贤惠指甲油,这篇文章主要介绍An entity object cannot be referenced by multiple instances of IEntityChangeTracker 的解决方案,现在分享给大家,希望可以做个参考。

使用EF对建立了关系的表新增记录时出现:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker一个实体对象不能由多个 IEntityChangeTracker 实例引用

 

在学习MVC+EF做demo时碰到的一个异常信息。在网上查了些,看得不是很明白,就自己折腾了一会儿。

先上出错的代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class CollegeInfo { private StudentManageContext stuCtx=new StudentManageContext(); public Models.CollegeInfoModel GetModel(Guid collegeId) { var model = stuCtx.CollegeInfoes.SingleOrDefault(s => s.CollegeId == collegeId); return model; } public int Add(Models.CollegeInfoModel model) { stuCtx.CollegeInfoes.Add(model); int count = stuCtx.SaveChanges(); return count; } public int Update(Models.CollegeInfoModel model) { Models.CollegeInfoModel oldModel = stuCtx.CollegeInfoes.Find(model.CollegeId); oldModel = model; stuCtx.Entry(oldModel).State = System.Data.EntityState.Modified; return stuCtx.SaveChanges(); } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class DepartmentInfo { private StudentManageContext stuCtx=new StudentManageContext(); public Models.DepartmentInfoModel GetModel(Guid departmentId) { var model = stuCtx.DepartmentInfoes.SingleOrDefault(s => s.DepartmentId == departmentId); return model; } public int Add(Models.DepartmentInfoModel model) { stuCtx.DepartmentInfoes.Add(model); int count = stuCtx.SaveChanges(); return count; } public int Update(Models.DepartmentInfoModel model) { Models.DepartmentInfoModel oldModel = stuCtx.DepartmentInfoes.Find(model.DepartmentId); oldModel = model; stuCtx.Entry(oldModel).State = System.Data.EntityState.Modified; return stuCtx.SaveChanges(); } }
复制代码
1
使用表关系图:
复制代码
1

两个表是一对多的关系。DepartmentInfo 中的collegeInfoId是外键。

 

添加DepartmentInfo代码:

复制代码
1
2
3
4
5
static void Main(string[] args) { Database.SetInitializer<StudentManageContext>(new DropCreateDatabaseIfModelChanges<StudentManageContext>());

CollegeInfoModel collegex = new BLL.CollegeInfo().GetModel(new Guid("B956CA6F-DD7D-4436-9099-484366A5F0B7"));

复制代码
1
2
3
4
5
6
7
8
9
DepartmentInfoModel depart = new DepartmentInfoModel() { DepartmentId = Guid.NewGuid(), DepartmentName = "depart" + DateTime.Now.ToString(), CollegeInfoObj = collegex }; new BLL.DepartmentInfo().Add(depart); }

这时会出现An entity object cannot be referenced by multiple instances of IEntityChangeTracker一个实体对象不能由多个 IEntityChangeTracker 实例引用的异常

 

原因是  CollegeInfoModel collegex = new BLL.CollegeInfo().GetModel(new Guid("B956CA6F-DD7D-4436-9099-484366A5F0B7"));  和  new BLL.DepartmentInfo().Add(depart); 这两句代码使用的是两个不同的StudentManageContext 对象造成的,可以仔细看看出错代码,两个类都有 private StudentManageContext stuCtx=new StudentManageContext(); ,到这儿你是否明白了呢。简单的来说,你要做的操作都必须在同一DbContext上完成。注:StudentManageContext派生于DbContext.

 

解决方案一:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static void Main(string[] args) { Database.SetInitializer<StudentManageContext>(new DropCreateDatabaseIfModelChanges<StudentManageContext>()); CollegeInfoModel collegex = new BLL.CollegeInfo().GetModel(new Guid("B956CA6F-DD7D-4436-9099-484366A5F0B7")); DepartmentInfoModel depart = new DepartmentInfoModel() { DepartmentId = Guid.NewGuid(), DepartmentName = "depart" + DateTime.Now.ToString(), }; //new BLL.DepartmentInfo().Add(depart); 将这句代码改成下面两句 collegex.Departments.Add(depart); new BLL.CollegeInfo().Update(collegex); }
复制代码
1
原因就是:Update时与GetModel用得时同一个DBContext。
复制代码
1
解决方案二:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class CollegeInfo:BaseModel { public Models.CollegeInfoModel GetModel(Guid collegeId) { var model = stuCtx.CollegeInfoes.SingleOrDefault(s => s.CollegeId == collegeId); return model; } public int Add(Models.CollegeInfoModel model) { stuCtx.CollegeInfoes.Add(model); int count = stuCtx.SaveChanges(); return count; } public int Update(Models.CollegeInfoModel model) { Models.CollegeInfoModel oldModel = stuCtx.CollegeInfoes.Find(model.CollegeId); oldModel = model; stuCtx.Entry(oldModel).State = System.Data.EntityState.Modified; return stuCtx.SaveChanges(); } } public class DepartmentInfo:BaseModel { public Models.DepartmentInfoModel GetModel(Guid departmentId) { var model = stuCtx.DepartmentInfoes.SingleOrDefault(s => s.DepartmentId == departmentId); return model; } public int Add(Models.DepartmentInfoModel model) { stuCtx.DepartmentInfoes.Add(model); int count = stuCtx.SaveChanges(); return count; } public int Update(Models.DepartmentInfoModel model) { Models.DepartmentInfoModel oldModel = stuCtx.DepartmentInfoes.Find(model.DepartmentId); oldModel = model; stuCtx.Entry(oldModel).State = System.Data.EntityState.Modified; return stuCtx.SaveChanges(); } } public abstract class BaseModel { protected StudentManageContext stuCtx = null; public BaseModel() { stuCtx = DataCache.GetCache("StudentManageContext") as StudentManageContext; if (stuCtx == null) { stuCtx = new StudentManageContext(); DataCache.SetCache("StudentManageContext", stuCtx); } } }

这样的话可以让CollegeInfo和DepartmentInfo共用同一个DBContext。

转载于:https://www.cnblogs.com/guolihao/p/3213723.html

最后

以上就是贤惠指甲油最近收集整理的关于An entity object cannot be referenced by multiple instances of IEntityChangeTracker 的解决方案的全部内容,更多相关An内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(109)

评论列表共有 0 条评论

立即
投稿
返回
顶部