在整个应用程序中使用静态类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();
}
}
}
}