简体   繁体   English

实体框架5:更改数据库连接

[英]Entity Framework 5 : changing the database connection

I have a made an EntityFramework model based on my Demo Database. 我有一个基于我的演示数据库的EntityFramework模型。

I want to include connection strings for Staging and production and then in my Console App prompt the user to see which database they want to perform the operation on. 我想包括用于登台和生产的连接字符串,然后在控制台应用程序中提示用户查看他们要在哪个数据库上执行操作。

When I was prompted to setup the EF.edmx file I just chose to store the connection string in the app.config file. 当提示我设置EF.edmx文件时,我只是选择将连接字符串存储在app.config文件中。 I have seen a link to change the Connection string of EntityFramework Context when initializing it here 在这里初始化时看到了更改EntityFramework Context的Connection字符串的链接

However when I store another connection to my Staging Database, I get an error "Keyword not supported: 'metadata'" 但是,当我存储另一个到暂存数据库的连接时,出现错误“不支持关键字:'元数据'”

So I stripped down the connection string not to include the EntityFramework type of parameters such as metadata=res://*/MyDBItems.csdl|res://*/MyDBItems.ssdl blah blah and used a very simple database connection string 因此,我删除了连接字符串,以不包括EntityFramework类型的参数,例如metadata=res://*/MyDBItems.csdl|res://*/MyDBItems.ssdl等等,并使用了非常简单的数据库连接字符串

data source=myDB;initial catalog=myDB;user id=myUser;password=myPass;MultipleActiveResultSets=True;

now my Context does instanciate but when I get to a query I get another error about: 现在我的上下文确实实例化了,但是当我查询时,我又遇到了另一个错误:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. 如果在Code First模式下使用T4模板为Database First和Model First开发生成的代码可能无法正常工作。 To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. 要继续使用数据库优先或模型优先,请确保在正在执行的应用程序的配置文件中指定了实体框架连接字符串。 To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception. 若要使用从Database First或Model First生成的这些类以及Code First,请使用属性或DbModelBuilder API添加任何其他配置,然后删除引发此异常的代码。

How can I switch between different databases with Entity Framework 5, thanks! 如何使用Entity Framework 5在不同的数据库之间切换,谢谢!

forget it I got it....what I did...I made a 2nd constructor in the Context.cs file 忘了我明白了......我做了什么...我在Context.cs文件中创建了第二个构造函数

    public MyContext(string dbModel) : base("name="+dbModel) {

    }

then in my app.config I have the various settings for the Demo,Staging & Production database connections....this took the full entityframework connection string. 然后在我的app.config中,对演示,登台和生产数据库连接进行了各种设置。...这花费了完整的entityframework连接字符串。 I think the link I provided was for Code-First but I am using Database First. 我认为我提供的链接是针对“代码优先”的,但我正在使用“数据库优先”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM