当前位置: 动力学知识库 > 问答 > 编程问答 >

vb.net - Change connection EF at runtime in WPF app

问题描述:

I have a WPF app that I'd like to change the connection string programmatically when the app loads. I use the Database-First approach for EF.

I spent a lot of time implementing various solutions found online including stack overflow and can't seem to get it to work.

The most common way seems to be to modify the Entity partial class. When I do this I get the following error at runtime:

Additional information: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

I got the same error implementing various other ways as well. If someone could please help me implement a way to change the connection string at runtime I'd greatly appreciate it.

My current implementation is taken from this example solution:

Changing Databases at Run-time using Entity Framework

Implementation:

  • App.config containts the connection string and name
  • Partial class added with same name as Entity class:

    Imports System.Data.Entity

    Imports System.Data.EntityClient

    Imports System.Data.SqlClient

    Partial Public Class MyEntityName

    Inherits DbContext

    Public Sub New(ByVal connString As String)

    MyBase.New(connString)

    End Sub

    End Class

In my Application.xaml code file I set a global string variable by calling a method that builds the EntityConnectionStringBuilder. This global string variable is then passed into an entity constructor.

Imports System.Reflection

Imports DevExpress.Xpf.Core

Imports System.Data.EntityClient

Class Application

Public Sub New()

entityConnStr = BuildConnectionString("[MyDataSource]", "[MyDatabase]")

End Sub

Private Function BuildConnectionString(ByVal DataSource As String, ByVal Database As String) As String

' Build the connection string from the provided datasource and database

Dim connString As String = "data source=" & DataSource & ";initial catalog=" & Database & ";persist security info=True;user id=[user];password=[password];trustservercertificate=True;MultipleActiveResultSets=True;App=EntityFramework""

' Build the MetaData... feel free to copy/paste it from the connection string in the config file.

Dim esb As New EntityConnectionStringBuilder()

esb.Metadata = "res://*/DB.[MyEntityName].csdl|res://*/DB.[MyEntityName].ssdl|res://*/DB.[MyEntityName].msl"

esb.Provider = "System.Data.SqlClient"

esb.ProviderConnectionString = connString

' Generate the full string and return it

Return esb.ToString()

End Function

Usage:

Using context = New MyEntity("entityConnStr")

Public connection variable string:

Public entityConnStr As String

网友答案:

I think you should remove the quotes when you pass the connection string to the constructor. You want to use the variable contents, not the variable name.

Use this:

Using context = New MyEntity(entityConnStr)

Instead of this:

Using context = New MyEntity("entityConnStr")
分享给朋友:
您可能感兴趣的文章:
随机阅读: