Get the Entity Framework Connection String

前端 未结 11 1093
迷失自我
迷失自我 2021-01-31 02:27

We use Entity Framework 5, but have a requirement to ALSO use a normal database connection from the application for some custom SQL we need to perform.

So, I am creating

11条回答
  •  误落风尘
    2021-01-31 03:03

    Here is a generic way of getting the connection string:

       public string GetConnString(T ent)
                where T : ObjectContext
            {
                EntityConnection ec = (EntityConnection)ent.Connection;
                SqlConnection sc = (SqlConnection)ec.StoreConnection;
                return sc.ConnectionString;
            }
    

    Then to use it would be:

     MyEntities ent = new MyEntities();
     String ConnString = GetConnString(ent);
    

提交回复
热议问题