Entity Framework getting an sql connection

后端 未结 8 1989
栀梦
栀梦 2021-02-08 02:16

In the light of Closing connections explicitly in Entity Framework and http://msdn.microsoft.com/en-us/library/bb738582%28v=vs.90%29.aspx it seems that I should be using the con

相关标签:
8条回答
  • 2021-02-08 02:24

    In EF5 (changed for EF6) the following would return the connection:

    var connection = ((EntityConnection)context.Connection).StoreConnection;

    If you are using EF in a right way you will probably never need to get inner db connection.

    0 讨论(0)
  • 2021-02-08 02:26

    You could use the following as well. I wanted to inject some Dapper.NET into a existing project that already had Entity framework entity context so I created the following:

    public class EFConnectionAccessor : IDisposable
    {
        private readonly SqlConnection sqlConnection;
        private readonly MyEntities entities;
    
        public EFConnectionAccessor()
        {
            entities = new MyEntities();
    
            var entityConnection = entities.Connection as EntityConnection;
    
            if (entityConnection != null)
            {
                sqlConnection = entityConnection.StoreConnection as SqlConnection;
            }
        }
    
        public SqlConnection connection
        {
            get
            {
                sqlConnection.Open();
                return sqlConnection;
            }
        }
    
        public void Dispose()
        {
            sqlConnection.Close();
            sqlConnection.Dispose();
            entities.Dispose();
        }
    }
    

    Called using

    using (SqlConnection sqlConnection = new EFConnectionAccessor().connection)
    {
     // ADO.NET CODE HERE - Or in my case dapper.net
    }
    
    0 讨论(0)
  • 2021-02-08 02:31

    the DataContext will allow you to call your Entity Objects right off of your DataContext reference, abstracting away all of the details of the underlying connection logistics.

    0 讨论(0)
  • 2021-02-08 02:33

    I used next code to getting connection (Entity Framework 5.0):

    var connection = Context.Database.Connection;
    
    0 讨论(0)
  • 2021-02-08 02:44
    var efConnectionStringBuilder = new EntityConnectionStringBuilder(efConnectionString);
    string sqlConnectionString = efConnectionStringBuilder.ProviderConnectionString;
    
    0 讨论(0)
  • 2021-02-08 02:47

    I found out that the magic lies in ExecuteStoreCommand()

      new AdventureEntities().ExecuteStoreCommand(
            @"    UPDATE Users
                  SET lname = @lname 
                  WHERE Id = @id",
            new SqlParameter("lname", lname), new SqlParameter("id", id));
    

    Then there is no need for an explicit Connection, it actually made the code a lot cleaner. The one-liner above replaced all of the following code

      using (SqlConnection con = new SqlConnection("Persist Security Info=False;Integrated Security=true;Initial Catalog=Remember;server=(local)"))
      {
        con.Open();
        using (SqlCommand cmd = con.CreateCommand())
        {
          cmd.CommandText = @"
              UPDATE Users
              SET lname = @lname 
              WHERE Id = @id";
          cmd.Parameters.AddWithValue("lname", lname);
          cmd.Parameters.AddWithValue("id", id);
          cmd.ExecuteNonQuery();
        }
      }
    
    0 讨论(0)
提交回复
热议问题