问题
I am trying to pick up ASP.Net - and following the mvc tutorial: http://www.asp.net/mvc/tutorials/mvc-music-store-part-4 (Note: I am totally new to the .Net framework, both C# and ASP.Net)
At the beginning the tutorial suggested to use SQL Server Compact 4.0 - But, I got the SQL Server Express installed instead (since I will be using it after, not just for the tutorial)
SELECT @@VERSION AS [Version]
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) Apr 2 2010 15:53:02 Copyright (c) Microsoft Corporation Express Edition with Advanced Services on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
Now, I am having trouble when I got to the point to set up the db connection for the tutorial. The tutorial step to add this to the Web.config:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
But that doesn't work - and pretty sure it's because the different SQL Server that I am using. Suggestion on how to make this work?
I've tried changing the providerName to "System.Data.SqlClient" and it still doesnt work, and I've tried the following too:
<connectionStrings>
<add
name="MusicStoreEntities"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
And it still doesnt work with the following exception: The provider did not return a ProviderManifestToken string
Help with setting up this ASP.Net - SQL Server Express 2008 would be greatly appreciated!
EDIT
Seems like now I got it working, at least to connect to the db:
Now, obviously the database is empty - the tutorial didnt even say about instantiating the database, other than downloading some asset file for the db "SampeData.cs", and add it to the Global.asax.cs Application_Start method:
System.Data.Entity.Database.SetInitializer(new MvcMusicStore.Models.SampleData());
And now, 2 things:
The line above in Application_Start dont seem to be hit when in debug mode (I did try rebuild solution)
Now I am getting the following error: Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
I think I am missing instantiating the data in the db - but not sure how / what - the tutorial doesnt say much
回答1:
I assume that you have restored a copy of the database to SqlExpress. Once you do that, you should add a Database element to the string with the name of the database you restored it as. A helpful resource for connectionstrings is http://www.connectionstrings.com
<connectionStrings>
<add
name="MusicStoreEntities"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Database=MusicStoreEntities"
providerName="System.Data.SqlClient" />
</connectionStrings>
Note that what you have is a link to an .sdf file, which is a SqlCE database. The |DataDirectory| portion is replaced with the location of your App_Data folder in your application.
回答2:
This is what I use:
<add name="<CONNECTION NAME>"
connectionString="Data Source=<SERVER NAME>;Initial Catalog=<DATABASE>;Persist Security Info=True;User ID=<USER ID>;Password=<PASSWORD>;"
providerName="System.Data.SqlClient" />
Replace <...> values with your corresponding values.
回答3:
I can never remember connection strings, I use this site to figure out what they need to be.
来源:https://stackoverflow.com/questions/6060643/asp-net-sql-connectionstrings-config