简体   繁体   English

更新数据库始终在实体框架中的。\\ SQLEXPRESS中更新

[英]Update-Database Always updating in .\SQLEXPRESS, in Entity Framework

I have a solution project in in which I have implemented Entity framework in a class library Project. 我有一个解决方案项目,其中在类库项目中实现了Entity框架。 I have a App.config file in the same class library in which I am giving my connection string as 我在同一个类库中有一个App.config文件,在其中我将连接字符串指定为

<connectionStrings>
<add name="DefaultConnection"
        connectionString="Data Source=DDC5-D-4R03T72;Initial Catalog=ServiceAdaptor;User ID=sa;Password=pwd;MultipleActiveResultSets=True;Integrated Security=False"

      providerName="System.Data.SqlClient"/>
 </connectionStrings>

(I am using Visual Studio 2010, and I am using Code-First Approach EF) (我正在使用Visual Studio 2010,并且正在使用代码优先方法EF)

Now the problem is when I run command "Update-Database -Verbose", It is not creating tables in above DB , rather it creates in .\\SQLExpress which I didn't mentioned in anywhere in my soln) 现在的问题是当我运行命令“ Update-Database -Verbose”时,它不是在上面的DB中创建表,而是在.\\SQLExpress创建,而我在soln的任何地方都没有提到过)

when I gave command like 当我发出命令

Update-Database -Verbose -ConnectionStringName "DefaultConnection"

it gave me message 它给了我消息

"No connection string named 'DefaultConnection' could be found in the application config file." “在应用程序配置文件中找不到名为'DefaultConnection'的连接字符串。”

What I conclude is my Update-Database command is not able to get the connection string from config file. 我的结论是我的Update-Database命令无法从配置文件中获取连接字符串。

Not able to figure out,What is wrong. 无法弄清楚,出了什么问题。

Finally I found the solution, There Problem was here that somehow Update-database command was searching Connection string in Web.Config file not in App.Config file as in my Case I have Class library project which has App.config file. 最后,我找到了解决方案,这里存在问题,就是更新数据库命令以某种方式在Web.Config文件中而不是App.Config文件中搜索连接字符串,因为在我的案例中,我有具有App.config文件的类库项目。

Since It did't get the Connection string By default EF framework in VS 10 will assume .\\SQLEXPRESS as default db and create the tables there. 由于它没有获得连接字符串,因此在VS 10中,默认情况下,EF框架将..SQLEXPRESS假定为默认数据库,并在其中创建表。

So the solution I found is: 所以我找到的解决方案是:

1) Add reference System.Configuration in the project 1)在项目中添加参考System.Configuration

2) Insteed of using base("DefaultConnection") in dbContext use as below 2)指导在dbContext中使用base(“ DefaultConnection”)如下

base(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString) 碱(System.Configuration.ConfigurationManager.ConnectionStrings [ “DefaultConnection”]。的ConnectionString)

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

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