如何让 ASP.NET MVC 3.0 站点使用默认 SQLEXPRESS 以外的数据库

发布于 2024-11-19 09:52:12 字数 1371 浏览 4 评论 0 原文

本质上我想做的是了解 Microsoft 的 ASP.NET MVC 3.0。我正在使用 Visual Studio 2010 Ultimate。我按照 http://www.asp 上的视频教程进行操作.net/mvc/videos/5-month-introduction-to-aspnet-mvc 让我启动并运行,结果创建了一个访问本地数据库的网站。

问题1:

有谁知道本地数据库在哪里?我相信它在某处使用默认的 SQLEXPRESS 数据库。它不会显示在服务器资源管理器中,也不在 App_Data 文件夹中。我在任何文件夹中都找不到它。据我了解,它是使用项目中的模型自动生成的,并且没有任何地方告诉我它放在哪里。

问题 2:

我真正想做的是将项目放到网上来玩,我已经通过 AppHarbor.com 做到了这一点。它们允许您创建一个 MS SQL Server 数据库,并为您提供一个替换连接字符串到 Web.config 中的 bung,但是替换了连接字符串并重建了解决方案 - 即使在将 AppHarbor 数据库(我很确定毫无意义)添加到服务器之后Explorer,我的网站仍然使用难以捉摸的 SQLEXPRESS 数据库,我找不到它。

我在 Web.config 中所做的更改来自:

  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

到格式

<connectionStrings>
    <add name="TestDB"
         connectionString="Server=SERVER;Database=DBNAME;User ID=USERID;Password=DBPASSWORD;" />         
</connectionStrings>

问题一很高兴知道,但问题 2 真正困扰着我。我错过了对数据库的哪些引用导致它使用了我不希望它使用且一无所知的数据库?我认为一定有一个,尤其是因为当我从 Web.config 中完全删除连接字符串时,事情仍然运行良好。

任何帮助将不胜感激。

essentially what I'm trying to do is get my head around Microsoft's ASP.NET MVC 3.0. I'm using Visual Studio 2010 Ultimate. I followed the video tutorial at http://www.asp.net/mvc/videos/5-minute-introduction-to-aspnet-mvc which got me up and running and as a result have created a website which accesses a local database.

Question 1:

Does anyone know where that local database is? I believe it's using a default SQLEXPRESS db somewhere. It doesn't show up in Server Explorer, it's not in the App_Data folder. I cant find it in any folder anywhere. It was - as I understand it - generated automatically using a model from the project and nowhere does it tell me where it's put it.

Question 2:

What I'd really like to do is put the project online to play about with, and I've done that already by way of AppHarbor.com. They let you create a MS SQL Server database and give you a replacement connection string to bung in Web.config, but having replaced the connection string and rebuilt the solution - even after adding the AppHarbor database (I'm pretty sure pointlessly) to Server Explorer, my site still uses the elusive SQLEXPRESS database which I cant find.

The change I made in Web.config was from:

  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

To the format

<connectionStrings>
    <add name="TestDB"
         connectionString="Server=SERVER;Database=DBNAME;User ID=USERID;Password=DBPASSWORD;" />         
</connectionStrings>

Question one would be nice to know, but it's question 2 that's really bothering me. What reference to the database have I missed that is causing it to use a database that I don't want it to and know nothing about? I figure that there must be one, not least because when I remove the connection string completely from Web.config the thing still runs along fine.

Any help would be greatly appreciated.

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

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

发布评论

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

评论(5

九歌凝 2024-11-26 09:52:12

所以我终于弄清楚了问题所在——在很大程度上得到了这里每个人的帮助。所以谢谢你。我主要做错了两件事。

首先,事实证明连接字符串的名称很重要。我需要根据我在 MVC 中设置的上下文来命名我的连接字符串。所以我的“TestDB”是错误的,对于我在问题中链接到的视频教程,连接字符串应该称为“SportsContext”。

所以最后,我的 Web.Config 连接字符串条目看起来像这样:

<connectionStrings>
<add name="SportsContext" connectionString="Server=SERVER;Database=DBNAME; User ID=USERID;Password=PASSWORD" providerName="System.Data.SqlClient" />
</connectionStrings>

有趣的是,这并不是他们告诉您应该在 appharbor 上使用的内容。其次,我忽略了另一个关键点,虽然 MVC 会很乐意在 SQLEXPRESS 中为您构建数据库,但它不会为 SQL Server 做同样的事情。当在 SQL Server 中自己构建它们时,您必须确保正确输入所有内容,包括自动递增 ID 列和外键等。

这两个更改是我的网站正常运行所需的全部内容。

So I finally worked out what the problem was - aided in no small part by everyone's help here. So thank you. There were two main things I had wrong.

Firstly, turns out that the name of the connection string is important. I needed to name my connection string after the context I'd set up in MVC. So my "TestDB" was wrong, and instead for the video tutorial I linked to in my question the connection string should be called "SportsContext".

So in the end, my connection string entry for Web.Config looked like this:

<connectionStrings>
<add name="SportsContext" connectionString="Server=SERVER;Database=DBNAME; User ID=USERID;Password=PASSWORD" providerName="System.Data.SqlClient" />
</connectionStrings>

Which interestingly enough is not what they tell you that you should use on appharbor. Secondly, and another key point I was missing, while MVC will happily build your databases for you in SQLEXPRESS, it won't do the same for SQL Server. And when building them yourself in SQL Server you have to be sure to get everything in correctly, including auto-incrementing ID columns and foreign keys etc.

Those two changes were all it took get my site up and working.

沦落红尘 2024-11-26 09:52:12

您需要首先运行 C:\WINDOWS\Microsoft.NET\Framework\\aspnet_regsql.exe

程序并将其指向远程(或本地)数据库

以下是概述此内容的链接 - http://msdn.microsoft.com/en-us/library/x28wfk74.aspx

然后只需更改 Web 配置条目以指向新数据库。

你需要使用

<add name="ApplicationServices" connectionString="DataSource=YourRemoteServerName;Initial
Catalog=YourDBName; Persist Security Info=True; UserID=YourUName;Password=YourPassword providerName="System.Data.SqlClient"
/>

You need to first run the C:\WINDOWS\Microsoft.NET\Framework\\aspnet_regsql.exe

program and point it to your remote (or local) database

Here is a link outlining this - http://msdn.microsoft.com/en-us/library/x28wfk74.aspx

then just change the web config entry to point to the new database.

You need to use

<add name="ApplicationServices" connectionString="DataSource=YourRemoteServerName;Initial
Catalog=YourDBName; Persist Security Info=True; UserID=YourUName;Password=YourPassword providerName="System.Data.SqlClient"
/>
情释 2024-11-26 09:52:12

有几点需要注意...

  1. 元素中,您可以尝试 在定义任何其他连接字符串之前,通过 清除任何现有的连接字符串。这将清除可能导致问题的任何继承的连接字符串。
  2. 既然您使用的是 MVC3,我就假设您使用的是实体框架?确保您的 DbContext (ef 4.1) 或任何其他 Context 对象已正确配置为使用“TestDB”。

至于 SQLExpress 数据库所在的位置,从原始连接字符串来看,它似乎正在查看本地 SQLExpress 服务器。

A couple things to look for...

  1. In the <ConnectionStrings> element, you may try clearing any existing connection strings, by way of <clear />, before defining any other connection strings. This will clear any inherited connection strings that may be causing the problem.
  2. Since you are using MVC3, I am going to assume that you are using Entity Framework? Ensure that either your DbContext (ef 4.1) or any other Context object is being properly configured to use "TestDB".

As for where the SQLExpress db is located, from the original connection string, it appears it is looking at your local SQLExpress server.

云朵有点甜 2024-11-26 09:52:12

当我尝试使用 appharbor 进行部署时,我遇到了同样的问题。查看您的项目目录 - 您实际上可能有三个 web.config 文件 - web.config、web.config.debug 和 web.config.release。 Visual Studio 2010 使用基于项目生成状态的转换来决定应用哪些转换。查看 appharbor 的转换测试器来了解这一点出去。

另外要小心,因为如果您在实体框架项目中设置错误,则每次更改模型时都可能会删除 appharbor 上的数据库。并且您获得凭据的帐户没有重新创建该数据库的权限。

如果您使用内置的 aspnet 用户模型,请确保使用 appharbor 数据库而不是本地 sqlexpress 来设置它们。这是关于此的操作方法

I had this same problem when I tried to deploy with appharbor. Look in your project directory - you probably actually have THREE web.config files - a web.config, web.config.debug and web.config.release. Visual Studio 2010 uses transformations based on your project build state to decide what transformations to apply. Check out the transformation tester from appharbor to get this figured out.

Also be careful because if you have the wrong settings in an Entity Framework project, you may be deleting the database on appharbor every time you make changes to your model. And the account that you have been given the credentials to does not have permission to re-create that database.

If you are using the built-in aspnet user models, make sure you set them up with the appharbor database instead of your local sqlexpress. Here is a How-To on that.

请你别敷衍 2024-11-26 09:52:12

问题 1:

在 Windows->开始->运行框中键入 services.msc,并确保 SQL EXPRESS 服务已启动。

在 VS 的服务器资源管理器中:连接到数据库,选择 Sql Server,输入此作为您的服务器: .\SQLEXPRESS 然后您应该能够测试您的连接。

问题 2:嗯,我可以通过尝试通过 web.config 从 SQLEXPRESS 更改为我的默认 SQL 实例来重现您的相同问题。 EF 不断在 SQLEXPRESS 中创建数据库。有趣,但我不知道。

Question 1:

Type services.msc in your Windows->Start->Run box and ensure that the SQL EXPRESS service is started.

In your Server Explorer in VS: Connect to Database, choose Sql Server, enter this as your server: .\SQLEXPRESS You should then be able to test your connection.

Question 2: HMMM i can reproduce your same issue by just attempting to change from SQLEXPRESS to my default SQL instance via web.config . EF keeps creating the DB in SQLEXPRESS. Interesting but i don't have a clue.

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