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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Basil
  • 1,613
  • 12
  • 25
  • Please supply: EF version, which exception, and indicate at which line of code the exception is thrown. – Gert Arnold Jan 02 '20 at 09:53
  • updated the question, sorry – Basil Jan 02 '20 at 09:58
  • Since this is all your own custom code and nothing standard, it's extremely difficult to really help you with anything ..... – marc_s Jan 02 '20 at 10:03
  • I'm just looking for a solution to open the EF connection without a connection string. – Basil Jan 02 '20 at 10:04
  • Have you read [this question](https://stackoverflow.com/questions/518535/how-to-use-ado-net-entity-framework-with-an-existing-sqlconnection)? I would hope so, given that it was the second result when I searched the web for your error message. – jmcilhinney Jan 02 '20 at 11:00
  • thanks for the link but i already read it and it didn't solve my issue :( – Basil Jan 02 '20 at 11:05
  • If your dll returns an opened SqlConnection, can't you access the ConnectionString property from that? – F0r3v3r-A-N00b Jan 02 '20 at 13:17
  • No, they remove it but the object connection is opened. They prevent developers from knowing the database credentials – Basil Jan 02 '20 at 13:18
  • "No, they remove it but the object connection is opened. " I would be interested to know how they do that. You can't open a connection without a connection string and once open you can't change it. They don't trust the developers with a connection string but they trust them to close and dispose an open connection. Wow! – Mary Jan 02 '20 at 16:34
  • We have different teams working on the project each team has a limited access, this is the procedure in our company, in the development server we have full access but in the production there is some limitations, and the SqlConnection connection string can easily be removed after opening the connection, why the WOW? – Basil Jan 02 '20 at 18:56
  • By the way what i mean by removing the connection string is “removing the password from the connection string” – Basil Jan 02 '20 at 19:01

1 Answers1

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

Basil
  • 1,613
  • 12
  • 25