Derik Whittaker



Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Linq2Sql issue with DateTime fields


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

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 };
		const bool expectedResponse = true;

		bool actualResponse = repository.CreateAccessKeyInformation(accessKey);
		Assert.That(actualResponse, Is.EqualTo(expectedResponse));

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,

Posted 03-27-2008 9:52 AM by Derik Whittaker



Mike Thomas wrote re: Linq issue with DateTime fields
on 03-27-2008 11:51 AM

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.

Peter Ritchie wrote re: Linq issue with DateTime fields
on 03-27-2008 11:53 AM

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?

Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - March 28, 2008 | Alvin Ashcraft's Morning Dew
on 03-28-2008 9:38 AM

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

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)