0

I am working with two C# Winform applications that use Linq to SQL for database access. All of my database access and queries exist in a DLL project that is referenced and used by both of these applications. The first application synchronizes data between an accounting application and a SQL server database. The second application reads, writes, and updates records in the same database. Currently, the DataContext is not disposed in either application until the program exits. Therefore, application 1 (synchronizing external app with SQL DB) and application 2 (create, read, update SQL DB) each have a reference to the database library which contains the context and DAOs. The first time they need DB access, an instance of the context is instantiated inside of a Singleton. The exception that I have encountered and steps to re-produce it are below.

1) Start up application 1 2) Start up application 2 3) Run application 1 which basically updates records in the SQL database based on some external accounting system. 4) Attempt to update one of the records modified by application 1

The mapping for the column in exception below is:

<Column Name="TimeStamp" Type="System.Data.Linq.Binary" DbType="rowversion NOT NULL" CanBeNull="false" IsVersion="true" />

Also, Update Check is set to never. I realize that it may be a better design to dispose of the dc more frequently but, that is not an immediate option since customers are using this application. Is there a way to solve this problem?

03/14/2013 15:18:08 Error BEN-LAPTOP Scribble.Database.Utilities.Domain.Persistence.Repository`1.SaveAll System.Collections.ListDictionaryInternal
03/14/2013 15:18:08 Error BEN-LAPTOP Scribble.Database.Utilities.Domain.Persistence.Repository`1.SaveAll Value of member 'TimeStamp' of an object of type 'Boat' changed.
A member that is computed or generated by the database cannot be changed.
03/14/2013 15:18:08 Error BEN-LAPTOP Scribble.Database.Utilities.Domain.Persistence.Repository`1.SaveAll    at System.Data.Linq.ChangeProcessor.CheckForInvalidChanges(TrackedObject tracked)
   at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
   at Scribble.Database.Utilities.DomainClassesDataContext.Submit(RefreshMode refreshMode) in C:\Aaron\Dev\ScribbleUtilityProjects\Scribble.Database.Utilities\DomainClasses.cs:line 42
   at Scribble.Database.Utilities.DomainClassesDataContext.SubmitOverwriteDatabase() in C:\Aaron\Dev\ScribbleUtilityProjects\Scribble.Database.Utilities\DomainClasses.cs:line 19
   at Scribble.Database.Utilities.Domain.Persistence.Repository`1.SaveAll() in C:\Aaron\Dev\ScribbleUtilityProjects\Scribble.Database.Utilities\Domain\Persistence\Repository.cs:line 79
03/14/2013 15:18:08 Error BEN-LAPTOP MarineService.GlobalCollections.showAndLogErrors Value of member 'TimeStamp' of an object of type 'Boat' changed.
A member that is computed or generated by the database cannot be changed.
03/14/2013 15:18:08 Error BEN-LAPTOP MarineService.GlobalCollections.showAndLogErrors    at Scribble.Database.Utilities.Domain.Persistence.Repository`1.SaveAll() in C:\Aaron\Dev\ScribbleUtilityProjects\Scribble.Database.Utilities\Domain\Persistence\Repository.cs:line 101
   at Scribble.Database.Utilities.Domain.Persistence.Repository`1.UpdateEntity(T entity, Boolean attach) in C:\Aaron\Dev\ScribbleUtilityProjects\Scribble.Database.Utilities\Domain\Persistence\Repository.cs:line 50
   at MarineService.Tests.AddWorkOrderForm.SaveWorkOrder() in C:\Aaron\Dev\ScribbleApplicationProjects\PureService\AddWorkOrderForm.cs:line 1638
4

1 回答 1

2

你不希望你的上下文是一个单例。从MSDN上的 DataContext 页面:

通常,DataContext 实例旨在持续一个“工作单元”,但是您的应用程序定义了该术语。DataContext 是轻量级的,创建起来并不昂贵。典型的 LINQ to SQL 应用程序在方法范围内创建 DataContext 实例,或者作为表示一组相关数据库操作的逻辑集的短期类的成员。

于 2013-03-14T20:35:30.253 回答