I am having a strange problem: I have a quite simple Login(string user, string pass) method for user login which needs to save last login date of that user:
using (MyDbContext dbContext = new MyDbContext())
{
var dbUser = dbContext.IdentityUserLogins.SingleOrDefault(u =>
u.LoginProvider == login && u.ProviderKey == password);
if (dbUser != null)
{
dbUser.LastLoginDate = DateTime.Now;
dbContext.Entry(dbUser).State = System.Data.Entity.EntityState.Modified;
dbContext.SaveChanges();
var id = Convert.ToInt32(dbUser.UserId);
/*other stuff*/
}
}
When I run this from a regular debug, everything works fine. The user I enter is found, his LastLoginDate is updated successfully and its Autogenerated SysStartTime is updated automatically at SaveChanges()
But when I run this method from an xUnit test, it behaves strange. The test is simple, like this:
[Fact]
[Trait("Category", "MyCategory")]
public void LoginTest()
{
string user = /*user name*/
string pass = /*get password hash*/
UserModel user = handler.Login(user, pass);
Assert.NotNull(user);
}
It simply calls that method.
The problem occurs on SaveChanges() call, an exception is thrown:
Cannot update GENERATED ALWAYS columns in table 'AppIdentityUserLogin'
Our tables use these columns like SysStartTime, SysEndTime and RowVersion, but there are no problems with Entity Framework knowing, how to handle them, this is the only exception - this xUnit test.
The problem is most likely in the dbContext.Entry(dbUser)... line, because when I comment it out, the test passes normally. I even think it is not needed there, because without it everything updates the same in regular debug. EDIT: Both - xUnit tests and regular debug are using the same ConnectionString.
What could cause this strange behavior? I tried to find some pre-run config, some differences between accessing this method as a test debugger and as a regular debugger, but couldn't find anything.