实体框架代码首先尝试打开错误的数据库

发布于 2024-12-29 18:04:44 字数 717 浏览 7 评论 0原文

我正在编写 ASP MVC 3 应用程序。我的数据库使用 Sql Server 2008 R2。

我创建了数据模型和名为 EFDbContextDbContext。 我创建了名为 SportsStore 的数据库。

我的连接字符串是:

<add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" />

我运行应用程序,但没有显示任何数据。

我发现 EF 自动生成一个新数据库 SportsStore.Domain.Concrete.EFDbContext,但我希望它使用我之前创建的 SportsStore,并自动将我的模型属性映射到表列。 如果我禁用自动生成数据库功能,我会收到以下错误:

无法打开数据库“SportsStore.Domain.Concrete.EFDbContext” 登录请求

实体框架不应该尝试打开 SportsStore 数据库吗?为什么它要尝试打开那个?我的连接字符串有误吗?

I am writing an ASP MVC 3 application. I am using Sql Server 2008 R2 for my database.

I created my data model and my DbContext called EFDbContext.
I created my database which is named SportsStore.

My connection string is:

<add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" />

I run the app and no data is shown.

I figured out that EF automatically generates a new database SportsStore.Domain.Concrete.EFDbContext, but I want it to use SportsStore which I have previously created, and to automatically map my model properties to table columns.
If I disable the autogenerate database feature I get the following error:

Cannot open database "SportsStore.Domain.Concrete.EFDbContext"
requested by the login

Shouldn't Entity Framework try to open SportsStore database? Why is it trying to open that one? Do I have a mistaken connection string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迟到的我 2025-01-05 18:04:44

奇怪...你有没有尝试过这个连接字符串?我认为你需要 MARS 来使用 DbContext 做一些事情。

<add name="EFDbContext" connectionString="Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />

这些是与我的项目之一不同的唯一事情。您确定该类&连接字符串的名称完全一样吗?您是否尝试过这样命名连接字符串?

<add name="SportsStore.Domain.Concrete.EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" />

Strange... have you tried this for your connection string? I think you need MARS to do some things with DbContext.

<add name="EFDbContext" connectionString="Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />

Those are the only things different than one of my projects where this works. You are sure the class & connection string are named exactly alike? Did you try naming the connection string like this?

<add name="SportsStore.Domain.Concrete.EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" />
你如我软肋 2025-01-05 18:04:44

我只能给你一个建议。
当我在一个项目(通常是类库)中拥有 DbContext 和相应的连接字符串时,我遇到了类似的困难。但是,如果您在其他项目(例如 win 表单)中使用它,它会忽略后者。它尝试从启动项目的配置文件中获取连接字符串。

如果是这种情况,请尝试将连接字符串放入启动项目配置文件中。

I can give you just a suggestion.
I came accross with a similar difficulties when I have my DbContext and corresponding connectionstring in one project (usually class library). However it ignores the latter if you use it in other project like win forms. It tries to get the connection string from the startup project's config file.

If this is your case try to place your connection string in your startup project config file.

无声无音无过去 2025-01-05 18:04:44

嗯。我遇到了同样的错误。经过一番努力,我意识到我将连接字符串放入了错误的 Web.config 文件中。有两个 Web 配置文件。一个在视图文件夹下,另一个在项目下。您需要在中添加连接字符串项目文件夹下有一个

hmm.I was getting same error.after some effort i realized that i had put connection string in wrong Web.config file.There are two web config files.One under view folder and another under the project.You need to add connection string in one under the project folder

巡山小妖精 2025-01-05 18:04:44

我注释掉了默认的连接字符串并将其放在下面:

<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-20130509163734;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Mvc4Movie-20130509163734.mdf" providerName="System.Data.SqlClient" />-->
<add name="MoveieDBContext" ...

这使得 EF 忽略我的连接字符串并生成自己的连接字符串。取消注释默认连接使 EF 使用我的连接,一切正常。

I had commented out the default connection string and placed mine underneath:

<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-20130509163734;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Mvc4Movie-20130509163734.mdf" providerName="System.Data.SqlClient" />-->
<add name="MoveieDBContext" ...

This made EF ignore my connection string and generate its own. Uncommenting the default connection made EF use mine and it all worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文