Reputation: 2746
I am trying to get LINQ to SQL to persist changes to an attached object wherein the backing table has a DateTime column that I think should function for row versioning, as described here.
The table looks like this:
CREATE TABLE [dbo].[client](
[client_id] [int] IDENTITY(1,1) NOT NULL,
[client_address1] varchar(100) NULL,
/* snip */
[modified_date] datetime NOT NULL,
CONSTRAINT [PK_CLIENT] PRIMARY KEY CLUSTERED ([client_id] ASC) )
Auto Generated Value: True Auto-Sync: Always Nullable: False Primary Key: False Read Only: False Server Data Type: DateTime Source: modified_date Time Stamp: True Update Check: Never
[Column(Storage="_modified_date", AutoSync=AutoSync.Always,
DbType="DateTime", IsDbGenerated=true, IsVersion=true,
UpdateCheck=UpdateCheck.Never)]
var c = new client { client_id = idOfClientToSave };
c.client_address1 = uxAddress1.Text;
// The DataContext is initialized in the constructor
// of ClientDataAccess
using (var ClientData = new ClientDataAccess())
{
ClientData.SaveClient(c);
}
public int SaveClient(client c)
{
c.modified_date = DateTime.Now.ToUniversalTime();
if (c.client_id == 0)
{
_db.GetTable<client>().InsertOnSubmit(c);
}
else
{
_db.GetTable<client>().Attach(c, true);
}
try
{
_db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException)
{
foreach (var con in _db.ChangeConflicts)
{
con.Resolve(RefreshMode.OverwriteCurrentValues);
}
throw;
}
}
Any help would be greatly appreciated.
Upvotes: 3
Views: 1959
Reputation: 110151
Try loading the original object in behind the new version.
else
{
_db.GetTable<client>().Attach(c, true);
_db.Refresh(RefreshMode.KeepCurrentValues, c);
}
I've learned this technique from this article, in the section: concurrency patterns for Update (and Delete) operations.
Upvotes: 4