在 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 中做到这一点?

最佳答案

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

关于entity-framework - 如何从 EF Core 2.0 DbContext 获取 connectionString,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45322282/

10-15 13:23