【问题标题】:Facing an issue with Add or Update scenario面临添加或更新方案的问题
【发布时间】:2016-04-25 09:36:39
【问题描述】:

我正在尝试 write LINQ to Add or Update 但对我来说 EntityState.Modified 不起作用。

查看我的代码,如果您发现任何错误,请告诉我。 只有新记录插入有效,但更新无效。 我正在使用 EF 6.0。

dbContext _context = new dbContext();            
string userName = //getting this value from somewhere else;
string userRoleNo = //getting this value from somewhere else;
Student student = new Student 
{
    userName = userName, 
    userRoleNo = userRoleNo, 
    Date = DateTime.Now 
};
bool exist = _context.Students.Any(x => x.UserId == new Guid(userId));
if (exist)
    _context.Entry(Student).State = EntityState.Modified;               
else
    _context.Students.Add(student);

_context.SaveChanges(); 

【问题讨论】:

  • 存在的价值是什么?代码是否属于else部分?
  • 查看这种比较 GUID 的方式:stackoverflow.com/questions/5725044/…
  • Harry 存在为真,代码属于 if 部分。 _context.Entry(学生).State = EntityState.Modified;被执行但在表中什么都不做
  • 如果条目存在,您需要 Attach() 您创建的学生实例。看看:msdn.microsoft.com/en-us/data/jj592676.aspx
  • 我所做的与教程“将现有但已修改的实体附加到上下文”中提到的完全相同

标签: c# entity-framework linq


【解决方案1】:

我想应该是

db.Entry(student).State = EntityState.Modified

而不是

db.Entry(Student).State = EntityState.Modified

【讨论】:

    【解决方案2】:

    这样做的正确方法是

    using(_context = new dbContext()){    //This will dispose the context           
    string userName = //getting this value from somewhere else;
    string userRoleNo = //getting this value from somewhere else;
    Student student = _context.Students.FirstOrDefault(x => x.UserId == new Guid(userId));
    
    
    if (student != null)
     {
       student.userName = userName; 
       student.userRoleNo = userRoleNo; 
       student.Date = DateTime.Now ;
       _context.Entry(student).State = EntityState.Modified;     
     }          
     else
     {
          student = new Student () {
           userName = userName, 
           userRoleNo = userRoleNo, 
           Date = DateTime.Now 
        };
      _context.Students.Add(student);
    }
    _context.SaveChanges();
    }; 
    

    【讨论】:

      【解决方案3】:

      仅在是新学生时添加。如果没有,EF 将为您跟踪更改。这样您就可以将跟踪和附加到 EF。

      dbContext _context = new dbContext();     
      var student = _context.Students.FirstOrDefault(x => x.UserId == new Guid(userId)); // Get the existing student.
      bool exists = true;
      if(student == null){
          student = new Student();
          exists = false;
      }
      
      string userName = //getting this value from somewhere else;
      string userRoleNo = //getting this value from somewhere else;
      student.userName = userName; // Do you really want to reset this?
      student.userRoleNo = userRoleNo;
      student.Date = DateTime.Now:
      
      if(!exists){
          _context.Students.Add(student);
      }
      _context.SaveChanges(); 
      

      仅当您不从上下文中获取项目时才需要附加和EntityState.Modified。但在这种情况下,它将改为获取项目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-24
        • 1970-01-01
        • 2019-06-14
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        • 2011-11-07
        • 1970-01-01
        相关资源
        最近更新 更多