Extract connection string from an Entity Connection String

前端 未结 5 560
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 09:50

When creating and ADO.NET Entity Connection String you get something like



        
5条回答
  •  孤城傲影
    2021-02-05 10:08

    Assuming you have an instance of the ObjectContext (if you are using the built-in designer, your context derives from the EF ObjectContext class). You can cast the value of the ObjectContext.Connection property (which is a DbConnection) to an EntityConnection.

    The EntityConnection class has a property StoreConnection which is the actual DbConnection used to connect to the database. This one actually has the ConnectionString property set to the one you are looking for.

    Edit: Some sample code (Assign context to your ObjectContext):

    ObjectContext context = entities;
    EntityConnection entityConnection = context.Connection as EntityConnection;
    if (null != entityConnection)
    {
        Console.WriteLine(entityConnection.StoreConnection.ConnectionString);
    }
    

提交回复
热议问题