1

I need to open the connection for Entity Framework without a connection string.

Due to a security layer that we are using I'm, we don't allow to connect to SQL Server using connection string, So we have a DLL that returns an opened SqlConnection.

  • EF version: 6.2.0

Error:

MetadataWorkspace must have EdmItemCollection pre-registered.

I tried to give the entityConnection as SqlConnection but I get an error.

Sample code:

Public Shared Function getEntityConnection() As EntityConnection
      Dim workspace As New MetadataWorkspace()
      Return New EntityClient.EntityConnection(workspace, AppCommon.AppFunctions.AppGetSQLCon(True))
End Function

AppCommon.AppFunctions.AppGetSQLCon(True) is the function which returns the SqlConnection instance.

But it's not working, does anyone have a solution for this issue?

11
  • Please supply: EF version, which exception, and indicate at which line of code the exception is thrown. Commented Jan 2, 2020 at 9:53
  • updated the question, sorry Commented Jan 2, 2020 at 9:58
  • Since this is all your own custom code and nothing standard, it's extremely difficult to really help you with anything ..... Commented Jan 2, 2020 at 10:03
  • I'm just looking for a solution to open the EF connection without a connection string. Commented Jan 2, 2020 at 10:04
  • Have you read this question? I would hope so, given that it was the second result when I searched the web for your error message. Commented Jan 2, 2020 at 11:00

1 Answer 1

0

Finally I found the solution for connecting the entityframework without a connection string, So what you need to is the following:

  1. change the constructor of the entity `DbContext' to recive the connection from a function like this:
    Public Sub New()
        MyBase.New(getEntityConnection(), False)
    End Sub
  1. Then inside that function return an entity-connection object from an open sqlconnection obbject as the following:
Public Shared Function getEntityConnection() As EntityConnection
    Dim workspace As New MetadataWorkspace(New String() {"res://*/"}, New Assembly() {Assembly.GetExecutingAssembly()})
    Return New EntityClient.EntityConnection(workspace, getSqlConnectionObject())        
End Function

Now your entityframework is connected to the database without a connection string

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.