如何从EF Core 2.0 DbContext获取connectionString

时间:2017-07-26 09:06:06

标签: entity-framework .net-core entity-framework-core

在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中执行此操作?

1 个答案:

答案 0 :(得分:35)

var connectionString = ctx.Database.GetDbConnection().ConnectionString;