I am trying write LINQ to Add or Update
but for me EntityState.Modified
is not working.
Have a look at my code and let me know if you see any error. Only new record insert works but Update does not work. I am using 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();