Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Linq2Sql issue with DateTime fields

*****
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,



Comments

Mike Thomas said:

I don't know the syntax off hand (my fight with LINQ has been fought mostly in the designer) but is there some parameter like "Update Check"?  I remember having to set that to "Never" in the designer to make datetime fields work right.

# March 27, 2008 11:51 AM

Peter Ritchie said:

Is the DB field smalldatetime?

My guess is that DLINQ is thinking the field is different than the last time it updated it.

Does UpdateCheck=UpdateCheck.WhenChanged or UpdateCheck.Never on the Column attribute for the DateTime column help?

# March 27, 2008 11:53 AM

Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew

# March 28, 2008 9:38 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News