Asp.net mvc 3 使用带有EF 4.0和Ninject的mvc mini profiler
我正在尝试在基于EF4的应用程序中使用新的mvc mini profiler,但我不知道如何正确地连接到目标数据源 这就是我所能得到的Asp.net mvc 3 使用带有EF 4.0和Ninject的mvc mini profiler,asp.net-mvc-3,entity-framework-4,ninject,mvc-mini-profiler,Asp.net Mvc 3,Entity Framework 4,Ninject,Mvc Mini Profiler,我正在尝试在基于EF4的应用程序中使用新的mvc mini profiler,但我不知道如何正确地连接到目标数据源 这就是我所能得到的 Func<IMyContainer> createContainer = () => { var profiler = MiniProfiler.Current; if (profiler != null) { var rootConn = // ???? var conn = Prof
Func<IMyContainer> createContainer = () =>
{
var profiler = MiniProfiler.Current;
if (profiler != null)
{
var rootConn = // ????
var conn = ProfiledDbConnection.Get(rootConn);
return ObjectContextUtils.CreateObjectContext<MyContainer>(conn);
}
else
{
return new MyContainer();
}
};
kernel.Bind<IMyContainer>().ToMethod(ctx => createContainer()).InRequestScope();
Func createContainer=()=>
{
var profiler=MiniProfiler.Current;
if(探查器!=null)
{
变量rootConn=/????
var conn=ProfiledDbConnection.Get(rootConn);
返回ObjectContextTILS.CreateObjectContext(conn);
}
其他的
{
返回新的MyContainer();
}
};
kernel.Bind().ToMethod(ctx=>createContainer()).InRequestScope();
如何在没有容器本身的情况下连接到EF容器?我只想新建一个SqlConnection,只是连接字符串被包装在所有EF垃圾中。您必须直接初始化连接,如下所示:
var rootConn = new System.Data.SqlClient.SqlConnection(your_connection_string_minus_your_ef_junk);
这是一个性能稍好,但略为粗糙的解决方案,用于获取商店连接
public static DbConnection GetStoreConnection<T>() where T : System.Data.Objects.ObjectContext
{
return GetStoreConnection("name=" + typeof(T).Name);
}
public static DbConnection GetStoreConnection(string entityConnectionString)
{
DbConnection storeConnection;
// Let entity framework do the heavy-lifting to create the connection.
using (var connection = new EntityConnection(entityConnectionString))
{
// Steal the connection that EF created.
storeConnection = connection.StoreConnection;
// Make EF forget about the connection that we stole (HACK!)
connection.GetType().GetField("_storeConnection",
BindingFlags.NonPublic | BindingFlags.Instance).SetValue(connection, null);
// Return our shiny, new connection.
return storeConnection;
}
}
publicstaticdbconnection GetStoreConnection(),其中T:System.Data.Objects.ObjectContext
{
返回GetStoreConnection(“name=“+typeof(T).name);
}
公共静态数据库连接GetStoreConnection(字符串entityConnectionString)
{
数据库连接存储连接;
//让实体框架完成繁重的工作来创建连接。
使用(var连接=新的EntityConnection(entityConnectionString))
{
//窃取EF创建的连接。
storeConnection=connection.storeConnection;
//让EF忘记我们偷的连接(黑客!)
connection.GetType().GetField(“\u storeConnection”,
BindingFlags.NonPublic | BindingFlags.Instance).SetValue(连接,null);
//返回我们闪亮的新连接。
返回存储连接;
}
}
稍微不那么老套的方式:
private static SqlConnection GetConnection()
{
var connStr = ConfigurationManager.ConnectionStrings["ModelContainer"].ConnectionString;
var entityConnStr = new EntityConnectionStringBuilder(connStr);
return new SqlConnection(entityConnStr.ProviderConnectionString);
}
John Gietzen的修正案: 所有答案的组合应该适用于实体框架支持的任何支持存储
public static DbConnection GetStoreConnection<T>() where T : System.Data.Objects.ObjectContext
{
return GetStoreConnection("name=" + typeof(T).Name);
}
public static DbConnection GetStoreConnection(string entityConnectionString)
{
// Build the initial connection string.
var builder = new EntityConnectionStringBuilder(entityConnectionString);
// If the initial connection string refers to an entry in the configuration, load that as the builder.
object configName;
if (builder.TryGetValue("name", out configName))
{
var configEntry = WebConfigurationManager.ConnectionStrings[configName.ToString()];
builder = new EntityConnectionStringBuilder(configEntry.ConnectionString);
}
// Find the proper factory for the underlying connection.
var factory = DbProviderFactories.GetFactory(builder.Provider);
// Build the new connection.
DbConnection tempConnection = null;
try
{
tempConnection = factory.CreateConnection();
tempConnection.ConnectionString = builder.ProviderConnectionString;
var connection = tempConnection;
tempConnection = null;
return connection;
}
finally
{
// If creating of the connection failed, dispose the connection.
if (tempConnection != null)
{
tempConnection.Dispose();
}
}
}
publicstaticdbconnection GetStoreConnection(),其中T:System.Data.Objects.ObjectContext
{
返回GetStoreConnection(“name=“+typeof(T).name);
}
公共静态数据库连接GetStoreConnection(字符串entityConnectionString)
{
//构建初始连接字符串。
var builder=新的EntityConnectionStringBuilder(entityConnectionString);
//如果初始连接字符串引用配置中的条目,请将其作为生成器加载。
对象名称;
if(builder.TryGetValue(“name”,out configName))
{
var configEntry=WebConfigurationManager.ConnectionString[configName.ToString()];
builder=新的EntityConnectionStringBuilder(configEntry.ConnectionString);
}
//为基础连接找到合适的工厂。
var factory=DbProviderFactories.GetFactory(builder.Provider);
//建立新的连接。
DbConnection tempConnection=null;
尝试
{
tempConnection=factory.CreateConnection();
tempConnection.ConnectionString=builder.ProviderConnectionString;
var连接=临时连接;
tempConnection=null;
回路连接;
}
最后
{
//如果创建连接失败,请处理该连接。
if(tempConnection!=null)
{
tempConnection.Dispose();
}
}
}
你在找(MyObjectContext.Connection作为EntityConnection)。StoreConnection
?@Craig:没有,因为我还没有MyContainer
。这并不能确切地告诉我做这件事的“正确”方法。这对我来说也很好。只需建立SqlConnection并将其传递给ObjectContextUtils.CreateObjectContext(rootConn)。完成!将oraConn调整为新的OracleConnection(新的EntityConnectionStringBuilder(ConfigurationManager.ConnectionString(“edmEntities”).ConnectionString.ProviderConnectionString),这可能是迄今为止最好的。有没有一种方法可以“神奇地”确定我们是要使用SqlConnection类,还是。。。例如,OracleConnection类?实体连接字符串有一个Provider属性,因此您可以在switch语句中使用该属性,并根据提供程序以不同的方式实例化连接。然后只需将返回类型设置为DbConnection,我想您就可以开始了。我找到了一种更优雅的方法,请参见DbProviderFactories
类。