1
我已經搜索控制與我試圖實現搜索作爲用戶類型的東西。我使用Linq to SQL來針對數據庫發起查詢。雖然它通常工作正常,但當用戶輸入查詢的速度非常快時,會引發一些隨機的SqlException。這些是我最近偶然發現的兩條不同錯誤信息:使用相同的datacontext多個查詢拋出SqlException
當前命令發生嚴重錯誤。如果有的話,結果應該被丟棄。
無效嘗試呼叫讀取器關閉時讀取。
編輯:包括代碼
DataContextFactory類:
public DataContextFactory(IConnectionStringFactory connectionStringFactory)
{
this.dataContext = new RegionDataContext(connectionStringFactory.ConnectionString);
}
public DataContext Context
{
get { return this.dataContext; }
}
public void SaveAll()
{
this.dataContext.SubmitChanges();
}
註冊IDataContextFactory使用Unity
// Get connection string from Application.Current settings
ConnectionInfo connectionInfo = Application.Current.Properties["ConnectionInfo"] as ConnectionInfo;
// Register ConnectionStringFactory with Unity container as a Singleton
this.container.RegisterType<IConnectionStringFactory, ConnectionStringFactory>(new ContainerControlledLifetimeManager(),
new InjectionConstructor(connectionInfo.ConnectionString));
// Register DataContextFactory with Unity container
this.container.RegisterType<IDataContextFactory, DataContextFactory>();
連接字符串:
Data Source=.\SQLEXPRESS2008;User Instance=true;Integrated Security=true;AttachDbFilename=C:\client.mdf;MultipleActiveResultSets=true;
從倉庫類使用的DataContext:
// IDataContextFactory dependency is injected by Unity
public CompanyRepository(IDataContextFactory dataContextFactory)
{
this.dataContextFactory = dataContextFactory;
}
// return List<T> of companies
var results = this.dataContextFactory.Context.GetTable<CompanyEntity>()
.Join(this.dataContextFactory.Context.GetTable<RegionEntity>(),
c => c.regioncode,
r => r.regioncode,
(c, r) => new { c = c, r = r })
.Where(t => t.c.summary_region != null)
.Select(t => new { Id = t.c.compcode, Company = t.c.compname, Region = t.r.regionname }).ToList();
什麼是身邊的工作?
你可以給你如何使用DataContext的代碼示例? – Konamiman
@Konamiman增加了代碼示例。你現在可以檢查一下嗎? – Raj