使用带有连接字符串转换的Entity Framework 6.0支持多个数据库

时间:2017-11-23 19:38:26

标签: c# entity-framework asp.net-web-api entity-framework-6

我有一个使用EF 6.0进行数据库操作的Web API项目。我有3个不同的Azure SQL数据库(Dev,Test,Prod)。

我已经能够使用数据优先方法创建实体数据模型。

我已经使用VS2017的配置管理器来创建web.test配置文件,但连接字符串的转换不起作用。

目前,我的Sub myReplace() Dim myDataSheet As Worksheet Dim myReplaceSheet As Worksheet Dim myLastRow As Long Dim myRow As Long Dim myColumn As Long Dim myLastColumn As Long Dim myFind As String Dim myReplace As String ' Specify name of Data sheet Set myDataSheet = Sheets("Sheet1") ' Specify name of Sheet with list of replacements Set myReplaceSheet = Sheets("Sheet2") ' Assuming list to search start in column A on row 2, find last entry in list myLastRow = myReplaceSheet.Cells(Rows.Count, "A").End(xlUp).Row ' Assuming list of replacement start in column B on row 2, find last entry in list myLastColumn = myReplaceSheet.Cells(Columns.Count, "2").End(xlToLeft).Column Application.ScreenUpdating = False ' Loop through all list of replacments For myRow = 2 To myLastRow For myColumn = B To myLastColumn ' Get find and replace values (from columns A and B) myFind = myReplaceSheet.Cells(myRow, "A") myReplace = myReplaceSheet.Cells(myRow, myColumn) ' Start at top of data sheet and do replacements myDataSheet.Activate Range("A1").Select ' Ignore errors that result from finding no matches On Error Resume Next ' Do all replacements on column A of data sheet Columns("A:A").Replace What:=myFind, Replacement:=myReplace, LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False ' Reset error checking On Error GoTo 0 Next myColumn Next myRow Application.ScreenUpdating = True MsgBox "Replacements complete!" End Sub 文件具有指向Dev环境的连接字符串,如下所示:

web.config

<connectionStrings> <add name="ProjectDbEntity" connectionString="metadata=res://*/Data.ProjectEntityModel.csdl|res://*/Data.ProjectEntityModel.ssdl|res://*/Data.ProjectEntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=company-ai-Projectserver.database.windows.net;initial catalog=company.DB_Dev;persist security info=True;user id=Project;password=Password1234$$;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> 如下:

web.test.config

当我使用它来构建使用一个数据库实例的EF模型时,我无法弄清楚如何制作EF更改连接字符串,但在部署期间我想要使用其他数据库实例架构。

我一直在阅读以下post,但不知何故无法使其发挥作用。

编辑:我找到了解决方案。我所要做的就是选择Publish选项,然后选择Settings并再次在弹出窗口中选择Settings,最后从下拉选项中选择配置到所需的配置。除非明确选择,否则在主菜单上选择配置似乎不会发布相同的环境。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我所要做的就是选择Publish选项,然后选择Settings并再次在弹出窗口中选择Settings,最后从下拉选项中选择配置到所需的配置。除非明确选择

,否则在主菜单上选择配置似乎不会发布相同的环境