【发布时间】:2013-10-02 09:17:21
【问题描述】:
在整个应用程序中使用静态类tConfig.ConnectionString 下载必要的连接字符串。不幸的是,我需要能够根据引用是否指向TransactionScope 来修改连接。目前,我有这段代码,但静态类称我为 StackOverflow 错误。请帮助实现此类静态(或更好的解决方案)中的功能。
public static class tConfig
{
public static string ConnectionString
{
get {
if (System.Transactions.Transaction.Current != null)
return "ConnectionString with scope";
else
return "ConnectionString without scope";
}
}
}
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetData;
[OperationContract]
string GetDataWithScope;
}
public class MyService : IMyService
{
public string GetData
{
using (var context = new MyEntities(tConfig.ConnectionString)
{
return context.table1.where(x=>x.ID == 1).Select(x=> x.F_NAME).FirstOrDefault().ToString();
}
}
public string GetDataWithScope
{
using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(600)))
{
using (var context = new MyEntities(tConfig.ConnectionString)
{
return context.table1.where(x=>x.ID == 1).Select(x=> x.F_NAME).FirstOrDefault().ToString();
}
}
}
}
【问题讨论】:
-
为什么要用transactionScope进行select操作?
-
这只是一个例子。我将它用于 AddObject 并更新
标签: c# wcf connection-string transactionscope