简体   繁体   English

本地数据库Visual Studio 2017的连接问题

[英]Connection problem with local db Visual Studio 2017

I am creating my first local db with Visual Studio. 我正在用Visual Studio创建我的第一个本地数据库。

I created one project where I installed entity framework, "ConsoleApp4". 我创建了一个项目,其中安装了实体框架“ ConsoleApp4”。 Then I created a seperate lib "ClassLibrary" (dll) where I added a database (mdf). 然后,我创建了单独的库“ ClassLibrary”(dll),并在其中添加了数据库(mdf)。

I have also built a connection string in numerous ways and put in "ConsoleApp4" app.config. 我还以多种方式构建了连接字符串,并将其放入“ ConsoleApp4” app.config中。

I am using package manager console to add-migrations and then update database. 我正在使用程序包管理器控制台添加迁移,然后更新数据库。

The db is created however it is a new db (created a by entity framework) and not the db in the "classLibrary". 数据库已创建,但是它是新的数据库(由实体框架创建),而不是“ classLibrary”中的数据库。

How do I get entity framework to understand that it should connect to the db I created in the class library (and just add the new tables)? 如何获得实体框架以了解它应该连接到我在类库中创建的数据库(并仅添加新表)?

I'm working a project with .net and my connection string looks like this, maybe it can help you 我正在使用.net进行项目,我的连接字符串如下所示,也许可以为您提供帮助

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=yourdatasource;Initial Catalog=yourcatalogname;Integrated Security=True" 
         providerName="System.Data.SqlClient" />
</connectionStrings>`

Solved by assign the name of the connection string i the db context constructor. 通过在数据库上下文构造函数中分配连接字符串的名称来解决。

Now it works! 现在可以了!

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

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