*****
Update
*****
Thanks to the suggestion to Peter Ritchie, this issue is no more.
Add the following to your Column() attribute and all is well with the world.
UpdateCheck = UpdateCheck.WhenChanged
***** Back to the original post *****
Another day, another Linq issue...... Starting to get REAL old, REAL fast.
Today when I came in I noticed that some of my tests were failing. Upon inspection I realized that the BDBA's on the team had made some changes to one of the tables i was using. This was not a big issue to me as i am ok with this. But what did piss me off was how linq worked with my changes.
The DBA's had added 3 columns to the table [IsActive, CreatedBy, CreatedDate], along with changing some field types (varchar to char, etc). In order to consume these changes I needed to update my column mappings on my entity, this was not an issue. However when I went and re-ran my tests i received the following error:
"System.Data.Linq.ChangeConflictException: Row not found or changed."
Below is my entity with mappings
public partial class AccessKey
{
[Column(AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public Int32 AccessKeyID { get; set; }
[Column(Name = "AccessKey", DbType = "Char(32) NOT NULL", CanBeNull = false)]
public string Key { get; set; }
[Column(DbType = "SmallInt NOT NULL")]
public Int32 AccessKeyActionTypeID { get; set; }
[Column(DbType = "Int")]
public Int32 UserID { get; set; }
[Column(Name = "ProgID", DbType = "Int")]
public Int32 ProgramID { get; set; }
[Column(DbType = "VarChar(10)")]
public string PIN { get; set; }
[Column(DbType = "Int")]
public Int32 RecruitingID { get; set; }
[Column(DbType = "Bit NOT NULL", IsDbGenerated = true)]
public bool IsActive { get; set; }
[Column(DbType = "VarChar(128) NOT NULL", IsDbGenerated = true)]
public string CreatedBy { get; set; }
[Column(AutoSync = AutoSync.OnUpdate, DbType = "DateTime NOT NULL", IsDbGenerated = true)]
public string CreatedDate { get; set; }
}
Below is my tests code
[Test]
public void CreateAccessKeyInformation_Valid()
AccessKeyRepository repository = new AccessKeyRepository();
Domain.Entities.AccessKey accessKey = new Domain.Entities.AccessKey { AccessKeyActionTypeID = _tempActionTypeForTests.AccessKeyActionTypeID, Key = keyGenerator.GenerateNewUniqueAccessKey(), PIN = "ertyui", ProgramID = 1, RecruitingID = 2, UserID = 3 };
try
{
const bool expectedResponse = true;
bool actualResponse = repository.CreateAccessKeyInformation(accessKey);
Assert.That(actualResponse, Is.EqualTo(expectedResponse));
}
finally
{
repository.DBContextInstance().AccessKey.DeleteOnSubmit(accessKey);
repository.DBContextInstance().SubmitChanges();
}
}
The goal of the test is to ensure i can create and insert valid data. After the insert is complete I would like to delete the data (good little testers clean up after them selves).
When I would call the SubmitChanges() method (inside the CreateAccessKeyInformation method) it would throw the above exception. Upon Googling the issue I came across this post (here). It appears that Linq has issues with DateTime fields and my Field CreatedDate is causing the issue.
However, the recommend suggestion from the post on how to resolve this DID NOT work for me. I tried to AutoSync atrribute to 'Always', 'OnInsert' and 'OnUpdate', none of them worked. Finally I just removed the column as it is not needed in any known business case. Although this works for me now, this is WEAK. I hope there is a way around this in the future.
If anyone else knows how to solve this, please let me know.
Till next time,