locked
Entity Framework 5 Unable to Set Connection In Code RRS feed

  • Question

  • In the previous versions of Entity Framework, I was able to set the EntityConnection in code.

    Has this been changed in EntityFramework 5?

    The new constructor of the EntityContainer does not accept the code created EntityConnection in it's signature.

    example: http://msdn.microsoft.com/en-us/library/vstudio/bb738461(v=vs.100).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

    Added ---  _data = New RequestDataEntityContainer(_conn)
     is where the problem happens since EntityFramework 5. It no longer accepts a connection here.

            Private _data As RequestDataEntityContainer
            Private _conn As EntityConnection
    
            Public Sub New(ByVal srv As Server)
    
                Dim providerName As String = "System.Data.SqlClient"
                Dim serverName As String = "ServerName"
                Dim databaseName As String = "DBName"
    
                Dim sqlBuilder As New SqlConnectionStringBuilder
                With sqlBuilder
                    .DataSource = serverName
                    .InitialCatalog = databaseName
                    .IntegratedSecurity = False
                    .PersistSecurityInfo = True
                    .ApplicationName = "AppName"
                    .UserID = "UserID"
                    .Password = "PW"
                    .MultipleActiveResultSets = True
                End With
    
                Dim providerString As String = sqlBuilder.ToString
                Dim entityBuilder As New EntityConnectionStringBuilder
                With entityBuilder
                    .Provider = providerName
                    .ProviderConnectionString = providerString
                    .Metadata = "res://*/RequestDataEntity.csdl|res://*/RequestDataEntity.ssdl|res://*/RequestDataEntity.msl"
                End With
    
                _conn = New EntityConnection(entityBuilder.ToString)
    
                _conn.Open()
    
                _data = New RequestDataEntityContainer(_conn)
    
            End Sub




    • Edited by TaikoKiasei Tuesday, March 12, 2013 6:38 PM
    Tuesday, March 12, 2013 4:44 PM

All replies

  • Hi,

    A bit hard to tell without any context but could it be a confusion between ObjectContext and DbContext ? DbContext wraps ObjectContext, adds support for Code First and is the new default for Database First/Model First starting with VS 2012...

    Double check your context is ObjectContext rather than DbContext based...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Tuesday, March 12, 2013 6:02 PM
  • I added more details and code sample to better clarify the "issue".

    I appreciate your time and help.


    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.

    Tuesday, March 12, 2013 6:39 PM
  • In addition please always post the (compile time ?) error message you get when you have one. Else one could try to repo the issue and run into some other error without knowing this is not what you get. It could also give some search direction.

    Finally please be explicit. Have you checked if you really used ObjectContext ? Have you checked from which type RequestDataEntityContainer inherits ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Wednesday, March 13, 2013 9:30 AM