how to setup MVC application to use sql server 2008 r2 express - sql-server

I am trying to follow the tutorial on the building my first MVC application for here
all was good until I needed to run the application an entity framework should have done it's thing and create the data base for me, but for some reason I always get the same error :
The provider did not return a ProviderManifestToken string.
my question:
what are the necessary adjustments i need to do in order for the application to work?
here is my web.config file connection string section
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=Moran-Laptop;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext"
connectionString="Data Source=|DataDirectory|Movies.sdf"
providerName="System.Data.SqlServerCe.4.0"/>

I think your data source is not correct. Does it work with:
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />

If you inspect the exception you will find out that there is InnerException as well which most probably points to SqlException and its inability to find the database or server. Your ConnectionString expects that you have Sql Server CE database available in your App_Data folder. The tutorial that you are looking does not yet tell you that you have to add a new SQL Server CE database to your App_Data folder. Check part 5 of the tutorial http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/getting-started-with-mvc3-part5-cs

First off you need to change your connection strings to point to the servers instance of sql server rather than to your laptop
in your connection strings replace Moran-laptop with "server-name\db name" i.e. r2008sqlserver\movies

Related

what is property of connectionString to connect SQL database to my MVC 5 web-app?

I'm learning MVC 5 and try to programming a web application. I'm following a toturial, but the problem is I can't fit the ConnectionString with may dataBase which is SQL Server.
This is a part of my web.config file:
<connectionStrings>
<add name="MovieDBContext"
connectionString="Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\Movies.mdf; Integrated Security=True"
providerName="System.Data.SqlClient" />
I think this is wrong beacuse it said that the source can not be found when I run the codes.
I have to know the whole elements of connectionString tag and what they mean.
Thanks everybody

How to transfer ASP.NET MVC Database from LocalDb to SQL Server?

I created a new ASP.NET MVC 5 project in Visual Studio 2013 (Express for Web) and by default, the project uses LocalDb as its database, but how do you transfer or migrate the database to SQL Server?
I want to use SQL Server for the database instead of LocalDb. But how?
Notwithstanding this question is old, the answer didn't help me so I want to share how I solved it for my self.
On Server Explorer, find your ASPNet DB. Then open it using SQL Server Object Explorer.
Then go and hit Schema Compare option
Then on the the Schema Compare window for the Target database, select the SQL Server data base you want the ASPNet DB to integrate to. Then hit Compare button
Deselect all Delete actions for the target database, and leave selected all Add actions for the ASPNet DB, then hit Update button.
Finally, update your connection string so it points to your SQL Server DB
Got it!
Based on #warheat1990's answer, you just have to change the connection string. But #warheat1990's answer had a little too much change. So here's my original (LocalDb) connection string:
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-my_project-20150318100658.mdf;Initial Catalog=my_project-20150318100658;Integrated Security=True"
providerName="System.Data.SqlClient"/>
To connect it to SQL Server instead of LocalDB, I modified the connection string into:
<add name="DefaultConnection"
connectionString="Data Source=SERVERNAME\SQLEXPRESS;Initial Catalog=my_project;Integrated Security=True"
providerName="System.Data.SqlClient"/>
Thanks to #warheat1990 for the idea of simply changing the Web.config. My first thoughts were to identify and use the feature that VS supplies, if theres any. Because Microsoft doesnt have a concise documentation on how to do this.
Change the connectionString in your web.config
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-KlikRX-20141203034323.mdf;Initial Catalog=aspnet-Test-20141203034323;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
to your own database connectionString, for example :
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=7.7.7.7\sql;Initial Catalog=TestDB;User ID=sa;Password=sa" />
</connectionStrings>
It sounds like you may want to move the data from your local database to sql server. If so, the easiest way to do this would be to back up your local database and then restore it on the server.
To back up:
https://msdn.microsoft.com/en-us/library/ms187510.aspx#SSMSProcedure
To restore:
https://msdn.microsoft.com/en-us/library/ms177429.aspx
EDIT:
If you need to install an instance of SQL Server:
https://msdn.microsoft.com/en-us/library/ms143219.aspx
Overlord's migration example is spot on. My note at the end was a bit big for a comment, so here are the required changes to the web.config file. An old method on a local drive was to specify
Data Source=".\[InstanceName]
but may not work on newer interfaces, so replace [.\instance] with [ComputerName\instance] if you migrate forward. This is Visual Studio Pro 2017, SQL Server 2014 & Entity Framework 6.0.
1st update the connection string.. replace items in brackets with info needed to connect to your database.
<connectionStrings>
<add name="DefaultConnection"
connectionString="Initial Catalog=[DatabaseName];Integrated Security=True;User ID=[SQLASPNETUserName];Password=[UserPassword];"
providerName="System.Data.SqlClient" />
</connectionStrings>
next update the entity info.. The [InstanceName] used for SQL Server can be found from [SQL Server Mgmt] console - [Server Properties] - [Advanced] - [Filestream Share Name] & defaults as [MSSQLSERVER].
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="[ServerName]\[InstanceName]"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
for cloud or other multi-server database migrations, also review [sessionState] settings in web.config & replace [InProc] with [Custom]. [sessionState] comes between [/roleManager] & [/system.web]
this default for 1 db server
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider"
type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
connectionStringName="DefaultConnection"/>
</providers>
</sessionState>
& this replacement for mult-server or cloud environments
<sessionState mode="Custom" customProvider="DefaultSessionProvider">
I had the same problem and just solved this...so the main point is default connection string...which you need to modify correctly otherwise it is pointless..and impossible to connect properly. So copy all you aspnetroles...users table to online database( they should look the same as in your local database).
You can compare schema(local db) with real db. It is quit well explained by "Overlord" -> Explanation
But after lets now correctly modify defaultconnection string
That is my default string before modification:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-track_spa-20180502025513.mdf;Initial Catalog=aspnet-track_spa-20180502025513;Integrated Security=True" providerName="System.Data.SqlClient" />
That is my modified default string after modification:
<add name="DefaultConnection" connectionString="Data Source=servername,portnumber;Initial Catalog=AttendanceTrak;Integrated Security=False;User Id=****;Password=*****;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
servername - should be your server.
portnumber - should be your server port
It took me ages to finally get it working properly...but this small trick with default string just made it!
Hops this helps
In relation to OverLords answer, it worked perfectly for me thanks!
If anyone is struggling with the connection string use:
<add name="CONNECTIONSTRINGNAME" connectionString='data source= DATABASE SOURCE initial catalog="DATABASE NAME ";user id="USERID";password=PASSWORD;MultipleActiveResultSets=True;' providerName="System.Data.SqlClient" />
I had a similar problem, wanting to export from a local db to remote db-server and encountered an error message I couldn't find any information on, but the answer came to me when reading this post, so I'm submitting my answer here in case anyone else has the same problem.
I set up a solution with Individual User Accounts. VS conveniently creates a db (mdf-file under App_Data) and a connectionstring in the web.config.
In all my wisdom I thought: "Why not move this to a remote server?" So I did.
I restored the mdf file on the remote server, expanded it with some simple tables for my web site, created a new connection to the db and added a new ado.net edmx-file, removed the "DefaultConnection" in the web.config and updated the reference to my new connection in the ApplicationDBContext.
Pressed play, and... no sigar (when trying to log in).
The entity type IdentityUserLogin is not part of the model for the current context.
Turns out the IdentityDbContex prefers the "DefaultConnection" with the providerName="System.Data.SqlClient" so adding a new edmx-file with the providerName="System.Data.EntityClient" is no good.
Solution: As warheat1990 suggested, I updated (put back) the DefaultConnections and it's connectionstring value.
One might argue that I should have two seperate db's (one for users) and one for business stuff, but that's an other discussion.
This works for me..
Change the connection string in the web config file pointing to the database server, then run the application and register a user. Once registered successfully, go to SSMS and refresh the database and then the identity tables should appear.
Regards

connection string is not working with my database on Microsoft SQL Server in ASP.NET MVC 4

I have this on my web.config file
<add name="EFDBContext"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=StadinPeli;
Integrated Security=True"
providerName="System.Data.SqlClient"/>
And yes in Server Explorer in visual studio 2012 professional edition, it works with
EFDBContext (WebUI).
But its the wrong database. Somehow this EFDBContext database copy its definitions from the database where I want it to connect all the time,
which is jon-pc\localdb#13158683.StadinPeli.dbo.
I checked properties from jon-pc\localdb#13158683.StadinPeli.dbo for its connection string
but it still connects to EFDBContext (WebUI).
Where is the problem, I have tried to correct this for 3 hours already.
(Localdb) as data source is probably wrong. Try updating it to (local) so your connection string looks like this
<add name="EFDBContext"
connectionString="Data Source=(local)\v11.0;Initial Catalog=StadinPeli;
Integrated Security=True"
providerName="System.Data.SqlClient"/>

Generate SQL Server Express database from Entity Framework 4 model

I am able to auto-generate a SQL Server CE 4.0 *.sdf file using code-first generation as explained by Scott Guthrie here. The connection string for the same is as follows:
<add name="NerdDinners" providerName="System.Data.SqlServerCe.4.0"
connectionString="data source=|DataDirectory|NerdDinner.sdf"/>
However if I try to generate an mdf instead using the following connection string, it fails to do so with the following error - "The provider did not return a ProviderManifestToken string.".
<add name="NerdDinners" providerName="System.Data.SqlClient" connectionString="data
source=|DataDirectory|NerdDinner.mdf"/>
Even directly hooking into a SQLEXPRESS instance using the following connection string fails
<add name="NerdDinners" providerName="System.Data.SqlClient" connectionString="Data
Source=.\SQLEXPRESS;Initial Catalog=NerdDinner;Integrated Security=True"/>
Does EF 4 only support SQL CE 4.0 for database creation from a model for now or am I doing something wrong here?
I was able to get this connection string to work with SQL Express on the same tutorial.
<add name="NerdDinners" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=NerdDinners; AttachDbFilename=|DataDirectory|NerdDinners.mdf; Integrated Security=True; User Instance=True" providerName="System.Data.SqlClient" />
Hope this helps someone in the future.
As a side note. If you're struggling to get the database to auto-create, it could well be because the database user you are using hasn't been given sufficient permissions. I was able to simply go the Security of the SQL Instance (not the database) and amend the Securables of the specific login so that it had the permission to Create Any Database (I didn't Grant any additional rights) and it allows EF4 to do its thing.

sql server express connection string

Is there a difference between sql server connection string and the express vesion?!
By default (though I don't recommend it1), Sql Express is installed as a named instance. That means you must connect like Server=servername.com\SQLEXPRESS as opposed to just Server=servername.com.
As always, connectionstrings.com has the goodies.
1 I don't recommend named instances because they run on a different port, which makes firewalling a PITA. Also, it makes little sense to me to run more than 1 MSSQL Server on a computer when you could just host multiple databases instead - so I'm not really sure what the point is.
Yes there is a difference- the big one being you won't have AttachDbFilename in the full SQL Server.
SQL Server Express connection string:
Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
Typical normal SQL Server connection string:
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
See connectionStrings.com for more info.
The connection string used to access SQL Server and SQL Server Express?
Generally, no. That's the nice thing about using Express; develop with it locally and with minimal changes migrate your data access to SQL Server.
The only difference is that you'll have to specify a named instance in the conn string, if Express was setup that way. 2005 had no option, but 2008 does.
Data Source=localhost\SQLExpress
Here's a great reference for connection strings.
I recently downloaded a sample application from the Telerik site and could not get it to run on my machine that has only SQL Server 2008 SP1 Developer Edition installed until I did a find and replace of all "User Instance=True" with "User Instance=False".
Before anybody goes and gets their panties all twisted in a knot, hear me out first. My circumstances may be (but ought not be) unique in that instead of installing SQL Server 2008 Express just to run example applications (e.g., Telerik), I simply installed a second named instance of SQL Server named, you guessed it, sqlexpress on my copy of SQL Server 2008 Developer Edition. That way when the Telerik (or you name it) demo application tries to connect to "Data Source=.\sqlexpress", it's none the wiser that what it's really connecting too is "big SQL Server" (which is what SQL Server Developer Edition is for the purposes of this posting).
The key conecpts (dare I say "money quote") you need to understand are these:
SQL Express allows (actually assumes if missing) User Instance=True, so if you are running a named instance called sqlexpress on "big SQL" (Developer, Standard, Enterprise or Data Center), then you just need to find/replace User Instance=True with User Instance=False in the connecton string and your worries are over. Even the "Attach database" stuff (that I see in the Telerik connectionStrings section of their web.config) works on "big SQL" (contrary to what somebody posted earlier in this thread).
Here's an actual example taken directly from the web.config of the Telerik web.config file after I "fixed it up":
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Northwind.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
<add name="TelerikConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Telerik.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
<add name="NorthwindConnectionString35" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Northwind.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
<add name="TelerikConnectionString35" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Telerik.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
<add name="NorthwindEntities" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Northwind.mdf;Integrated Security=True;User Instance=False;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
<add name="TelerikEntities" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Telerik.mdf;Integrated Security=True;User Instance=False;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
<add name="EditorFileBrowserDatabase" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|EditorDBStorage.mdf;Integrated Security=True;User Instance=False" providerName="System.Data.SqlClient"/>
</connectionStrings>
In addition to previous post: If you have troubles with setup Telerik samples on MSSQL Server 2008, you can do the following:
Open LiveDemos Telerik VS solution
Setup database connections for telerik .mdf files in VS master
Copy connection string for connections to the web.config file
And add "Integrated Security=True;User Instance=False" to the end of the strings.
PS. Fred Morrison - thanx, you are my "man of day", I just fix Telerik Live Demo problem with you post help!

Resources