none
Passing OracleConnection to DbContext RRS feed

  • Question

  • Hi,

    i have build an EF project using the database first approach and now i want to pass a DbConnection (OracleConnection) directly to the DbContext instead of using the App.Config`s connection string. Is this possible? If yes how?

    Cheers

    Christoph


    Monday, January 20, 2014 3:09 PM

Answers

  • Yes i did, but then the function onModelCreating gets called and throws a UnintentionalCodeFirstException.

    Meanwhile i solved the problem myself:

    I created the model by using the database first wizard, then I retrieved the corresponding MetadataWorkspace with :

    MetadataWorkspace ws = new MetadataWorkspace(new string[] { "res://*/" }, new Assembly[] { Assembly.GetExecutingAssembly() });

    Then I created an EntityConnection using the MetadataWorkspace and my DBConnection (OracleConnection):

    EntityConnection ecEntityConnection = new EntityConnection(ws, oDBConnectionTradenet);

    Which can be used to call the Constuctor you proposed:

    Entities oEntityContext = new Entities(ecEntityConnection, false)

    Et voila.. it works now..

    Thanks for your advices...

    • Marked as answer by Christoph_Merg Wednesday, January 22, 2014 12:45 PM
    Wednesday, January 22, 2014 12:45 PM

All replies

  • Yes.  Look at your generated DbContext subclass.  It should be defined as a partial class.  If so, create another partial class file for that type and add a constructor that chains to DbContext(DbConnection,bool)

    David


    David http://blogs.msdn.com/b/dbrowne/

    • Marked as answer by Christoph_Merg Wednesday, January 22, 2014 11:58 AM
    • Unmarked as answer by Christoph_Merg Wednesday, January 22, 2014 12:45 PM
    Monday, January 20, 2014 5:02 PM
  • Hello Christoph_Merg,

    For “Passing Connections to the Context”, you can also have a look link below:

    http://msdn.microsoft.com/en-us/data/dn456849#passing

    It shows how to work with DbConnection and its limitations.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Christoph_Merg Wednesday, January 22, 2014 11:58 AM
    • Unmarked as answer by Christoph_Merg Wednesday, January 22, 2014 11:59 AM
    Tuesday, January 21, 2014 2:08 AM
    Moderator
  • Hi David,

    thanks for your reply. I implemented the second partial class with the customized ctor and expected some kind of magic to happen.

    Of course it didn`d just work out of the box. Could you please specify your reply?

    How do i make the DbContext accept the auto-generated model and still use my passed DbConnection?

    Cheers

    Crhistoph


    Wednesday, January 22, 2014 10:02 AM
  • Did you change your calling code to use the new constructor?  Like:

    using (var conn = new SqlConnection("{connectionString}")) 
                { 
                    conn.Open(); 
     
                    var sqlCommand = new SqlCommand(); 
                    sqlCommand.Connection = conn; 
                    sqlCommand.CommandText = 
                        @"UPDATE Blogs SET Rating = 5" + 
                         " WHERE Name LIKE '%Entity Framework%'"; 
                    sqlCommand.ExecuteNonQuery(); 
     
                    using (var context = new BloggingContext(conn, contextOwnsConnection: false)) 
                    { 
                        var query = context.Posts.Where(p => p.Blog.Rating > 5); 
                        foreach (var post in query) 
                        { 
                            post.Title += "[Cool Blog]"; 
                        } 
                        context.SaveChanges(); 
                    } 
     
                    var sqlCommand2 = new SqlCommand(); 
                    sqlCommand2.Connection = conn; 
                    sqlCommand2.CommandText = 
                        @"UPDATE Blogs SET Rating = 7" + 
                         " WHERE Name LIKE '%Entity Framework Rocks%'"; 
                    sqlCommand2.ExecuteNonQuery(); 
                } 
    

    David


    David http://blogs.msdn.com/b/dbrowne/

    Wednesday, January 22, 2014 12:28 PM
  • Yes i did, but then the function onModelCreating gets called and throws a UnintentionalCodeFirstException.

    Meanwhile i solved the problem myself:

    I created the model by using the database first wizard, then I retrieved the corresponding MetadataWorkspace with :

    MetadataWorkspace ws = new MetadataWorkspace(new string[] { "res://*/" }, new Assembly[] { Assembly.GetExecutingAssembly() });

    Then I created an EntityConnection using the MetadataWorkspace and my DBConnection (OracleConnection):

    EntityConnection ecEntityConnection = new EntityConnection(ws, oDBConnectionTradenet);

    Which can be used to call the Constuctor you proposed:

    Entities oEntityContext = new Entities(ecEntityConnection, false)

    Et voila.. it works now..

    Thanks for your advices...

    • Marked as answer by Christoph_Merg Wednesday, January 22, 2014 12:45 PM
    Wednesday, January 22, 2014 12:45 PM