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.
本质上,我想要做的是让我的脑袋绕过微软的ASP。净MVC 3.0。我用的是Visual Studio 2010 Ultimate。我学习了http://www.asp.net/mvc/videos/5分钟介绍到aspnet-mvc的视频教程,它让我开始并运行,并创建了一个访问本地数据库的网站。
Question 1:
问题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.
有人知道本地数据库在哪里吗?我相信它在某处使用了一个默认的SQLEXPRESS db。它不在服务器资源管理器中显示,不在App_Data文件夹中。我在任何文件夹都找不到。正如我所理解的那样,它是使用项目中的一个模型自动生成的,它没有告诉我它放在哪里。
Question 2:
问题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.
我真正想做的是把这个项目放到网上去玩,我已经通过AppHarbor.com做过了。它们允许您创建一个MS SQL Server数据库,并为Web中的bung提供替换连接字符串。配置,但是已经替换了连接字符串并重新构建了解决方案——甚至在添加AppHarbor数据库(我非常肯定地)到服务器资源管理器之后,我的站点仍然使用我无法找到的难以理解的SQLEXPRESS数据库。
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.
第一个问题很好,但是第二个问题真的困扰着我。我漏掉了什么对数据库的引用,导致它使用一个我不希望它使用的数据库,并且对它一无所知?我认为必须有一个,尤其是因为当我从Web上完全删除连接字符串时。配置它仍然运行良好。
Any help would be greatly appreciated.
如有任何帮助,我们将不胜感激。
5 个解决方案
#1
3
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".
首先,发现连接字符串的名称很重要。我需要根据在MVC中设置的上下文来命名我的连接字符串。因此,我的“TestDB”是错误的,对于我在问题中链接到的视频教程,连接字符串应该被称为“SportsContext”。
So in the end, my connection string entry for Web.Config looked like this:
最后,我的Web连接字符串条目。配置看起来像这样:
<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.
有趣的是,他们并没有告诉你应该在appharbor上使用。其次,我缺少的另一个关键点是,MVC很乐意为您在SQLEXPRESS上构建您的数据库,它不会对SQL Server进行同样的操作。当您自己在SQL Server中构建它们时,您必须确保正确地获取所有内容,包括自动递增ID列和外键等等。
Those two changes were all it took get my site up and working.
这两个变化就是我的网站建立和工作所需要的。
#2
1
You need to first run the C:\WINDOWS\Microsoft.NET\Framework\\aspnet_regsql.exe
首先需要运行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
这里有一个链接,列出了这个链接:http://msdn.microsoft.com/en-us/library/x28wfk74.aspx。
then just change the web config entry to point to the new database.
然后只需将web配置条目更改为指向新的数据库。
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"
/>
#3
0
A couple things to look for...
有几件事要找……
- 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. -
在
元素中,您可以尝试在定义任何其他连接字符串之前,通过 清除任何现有的连接字符串。这将清除可能导致问题的任何继承的连接字符串。 - 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".
- 既然您正在使用MVC3,那么我假设您正在使用实体框架?确保您的DbContext (ef 4.1)或任何其他上下文对象被正确配置为使用“TestDB”。
As for where the SQLExpress db is located, from the original connection string, it appears it is looking at your local SQLExpress server.
至于SQLExpress db位于何处,从原始连接字符串来看,它似乎正在查看本地SQLExpress服务器。
#4
0
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.
当我尝试与appharbor一起部署时,我遇到了同样的问题。看看你的项目目录——你可能有三个网站。配置文件-一个web。配置,web.config.debug web.config.release。Visual Studio 2010使用基于项目构建状态的转换来决定要应用哪些转换。查看来自appharbor的转换测试器,了解这一点。
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.
同样要小心,因为如果在实体框架项目中设置了错误的设置,那么每次修改模型时,都可能会删除appharbor上的数据库。而您已获得证书的帐户没有重新创建该数据库的权限。
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.
如果您正在使用内置的aspnet用户模型,请确保使用appharbor数据库而不是本地sqlexpress设置它们。这里有一个操作方法。
#5
0
Question 1:
问题1:
Type services.msc in your Windows->Start->Run box and ensure that the SQL EXPRESS service is started.
服务类型。msc在您的Windows->启动->运行框中,确保启动SQL EXPRESS服务。
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.
在您的服务器资源管理器中的VS: Connect to Database中,选择Sql Server,输入这个作为您的服务器:.\SQLEXPRESS您应该能够测试您的连接。
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.
问题2:嗯,我可以通过尝试通过web从SQLEXPRESS更改到我的默认SQL实例来重现您的问题。配置。EF一直在SQLEXPRESS中创建数据库。很有趣,但我不知道。
#1
3
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".
首先,发现连接字符串的名称很重要。我需要根据在MVC中设置的上下文来命名我的连接字符串。因此,我的“TestDB”是错误的,对于我在问题中链接到的视频教程,连接字符串应该被称为“SportsContext”。
So in the end, my connection string entry for Web.Config looked like this:
最后,我的Web连接字符串条目。配置看起来像这样:
<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.
有趣的是,他们并没有告诉你应该在appharbor上使用。其次,我缺少的另一个关键点是,MVC很乐意为您在SQLEXPRESS上构建您的数据库,它不会对SQL Server进行同样的操作。当您自己在SQL Server中构建它们时,您必须确保正确地获取所有内容,包括自动递增ID列和外键等等。
Those two changes were all it took get my site up and working.
这两个变化就是我的网站建立和工作所需要的。
#2
1
You need to first run the C:\WINDOWS\Microsoft.NET\Framework\\aspnet_regsql.exe
首先需要运行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
这里有一个链接,列出了这个链接:http://msdn.microsoft.com/en-us/library/x28wfk74.aspx。
then just change the web config entry to point to the new database.
然后只需将web配置条目更改为指向新的数据库。
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"
/>
#3
0
A couple things to look for...
有几件事要找……
- 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. -
在
元素中,您可以尝试在定义任何其他连接字符串之前,通过 清除任何现有的连接字符串。这将清除可能导致问题的任何继承的连接字符串。 - 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".
- 既然您正在使用MVC3,那么我假设您正在使用实体框架?确保您的DbContext (ef 4.1)或任何其他上下文对象被正确配置为使用“TestDB”。
As for where the SQLExpress db is located, from the original connection string, it appears it is looking at your local SQLExpress server.
至于SQLExpress db位于何处,从原始连接字符串来看,它似乎正在查看本地SQLExpress服务器。
#4
0
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.
当我尝试与appharbor一起部署时,我遇到了同样的问题。看看你的项目目录——你可能有三个网站。配置文件-一个web。配置,web.config.debug web.config.release。Visual Studio 2010使用基于项目构建状态的转换来决定要应用哪些转换。查看来自appharbor的转换测试器,了解这一点。
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.
同样要小心,因为如果在实体框架项目中设置了错误的设置,那么每次修改模型时,都可能会删除appharbor上的数据库。而您已获得证书的帐户没有重新创建该数据库的权限。
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.
如果您正在使用内置的aspnet用户模型,请确保使用appharbor数据库而不是本地sqlexpress设置它们。这里有一个操作方法。
#5
0
Question 1:
问题1:
Type services.msc in your Windows->Start->Run box and ensure that the SQL EXPRESS service is started.
服务类型。msc在您的Windows->启动->运行框中,确保启动SQL EXPRESS服务。
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.
在您的服务器资源管理器中的VS: Connect to Database中,选择Sql Server,输入这个作为您的服务器:.\SQLEXPRESS您应该能够测试您的连接。
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.
问题2:嗯,我可以通过尝试通过web从SQLEXPRESS更改到我的默认SQL实例来重现您的问题。配置。EF一直在SQLEXPRESS中创建数据库。很有趣,但我不知道。