计划将项目中使用entity framework的要点记录到改栏目下

ef监控sql执行性能日志。http://www.cnblogs.com/CreateMyself/p/5277681.html

http://123.122.205.38/cn_sql_server_2008_r2_enterprise_x86_x64_ia64_dvd_522233.iso?fid=hqkUfQCwLLgefGdBiOC73ps1JxMA*O0VAQAAAGT1XOywsz1scWyaiQsnM*YF3JxX&mid=666&threshold=150&tid=E644E3AB354940E901996D7CF1F47CF9&srcid=119&verno=1

 

今天在做毕业设计时,遇到了一个很郁闷的问题:

public partial class ProjectModule : Entity
    {
        public ProjectModule()
        {
            Id = GuidComb.GenerateComb();
        }

        public Guid Id { get; set; }
        public string Name { get; set; }
        public int Grade { get; set; }
        public int DisplayOrder { get; set; }
        public string FullPathName { get; set; }

        public virtual Project Project { get; set; }
        public virtual ProjectModule Parent { get; set; }
    }

 我是用函数获取对象,对其进行修改:

1         public ProjectModule GetById(Guid id)
2         {
3             return _context.ProjectModule
4                 .FirstOrDefault(m => m.Id == id);
5         }

之后调用对象对其修改:

 1     [HttpPost]
 2         public JsonResult Modify(ProjectModuleViewModel model)
 3         {
 4             using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
 5             {
 6                 AjaxResult result = new AjaxResult();
 7 
 8                 ProjectModule module = _projectModuleService.GetById(model.Module_Id.Value);
 9                 if (model.Module_IsDelete == 1)
10                 {
11                     try
12                     {
13                         _projectModuleService.Delete(module);
14                         unitOfWork.Commit();
15 
16                         result = new AjaxResult() { status = AjaxResultStatus.success, detail = "删除成功!" };
17                     }
18                     catch (Exception ex)
19                     {
20                         unitOfWork.Rollback();
21                         LoggingService.Error(ex);
22                         result = new AjaxResult() { status = AjaxResultStatus.failed, detail = "删除失败!" };
23                     }
24 
25                     return Json(result);
26                 }
27 
28                 module.DisplayOrder = model.Module_DisplayOrder;
29                 module.Name = model.Module_Name;
30 
31                 Project project = _projectService.GetById(model.Module_ProjectId);               
32                 module.Project = project;
33 
34                 if (string.IsNullOrEmpty(model.Module_Name))
35                 {
36                     result = new AjaxResult() { status = AjaxResultStatus.failed, detail = new Dictionary<string, string>() };
37                     result.AddDetailItem("Module_Name", "请填写模块名");
38                     return Json(result);
39                 }
40 
41                 if (model.Module_ParentId.HasValue == false)
42                 {
43                     module.FullPathName = model.Module_Name;
44                     module.Parent = null;
45                     module.Grade = 0;
46                 }
47                 else
48                 {
49                     if (model.Module_ParentId.Value == model.Module_Id)
50                     {
51                         result = new AjaxResult() { status = AjaxResultStatus.failed, detail = new Dictionary<string, string>() };
52                         result.AddDetailItem("Module_ParentId", "节点自己不允许作为自己的子节点!");
53                         return Json(result);
54                     }
55                     ProjectModule parent = _projectModuleService.GetById(model.Module_ParentId.Value);
56 
57                     module.FullPathName = parent.FullPathName + "/" + model.Module_Name;
58                     module.Parent = parent;
59                     module.Grade = parent.Grade + 1;
60                 }
61 
62                 try
63                 {
64                     unitOfWork.Commit();
65 
66                     result = new AjaxResult() { status = AjaxResultStatus.success, detail = "保存成功!" };
67                 }
68                 catch (Exception ex)
69                 {
70                     unitOfWork.Rollback();
71                     LoggingService.Error(ex);
72                     result = new AjaxResult() { status = AjaxResultStatus.failed, detail = "保存失败!" };
73                 }
74 
75                 return Json(result);
76             }
77         }

 

其中代码执行了 module.Parent=null之后,发现并没有把Parent对象真的赋值为null,但我修改了之后发现其他字段都已经修改了,后来发现parent字段在数据库中依然存在有值。

后来尝试了使用我尝试修改啦GetById()函数后,发现问题解决了。

修改为:

  public ProjectModule GetById(Guid id)
        {
            return _context.ProjectModule
                .Include(x => x.Parent)
                .FirstOrDefault(m => m.Id == id);
        }

 

posted @ 2016-03-16 19:03  cctext  阅读(255)  评论(0编辑  收藏  举报