在EF6中使用此代码:
public string GetConnectionString(DbContext ctx)
{
ObjectContext _objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
if (_objectContext?.Connection != null)
{
EntityConnection entityConnection = _objectContext.Connection as EntityConnection;
return entityConnection?.StoreConnection?.ConnectionString;
}
return null;
}
如何在EF Core 2.0中执行此操作?
答案 0 :(得分:35)
var connectionString = ctx.Database.GetDbConnection().ConnectionString;