MVC Migrate IdentityUser SQL Server 2012 Migration - asp.net

Is it possible to change the IdentityUser to use a SQL Server 2012 database instead of the localdb that installs by default when creating a new ASP.NET Web Application?
I have an existing SQL Server 2012 database which I would like to use with the MVC 5 project, I can modify the connection string in the web config, however I get the error "The entity type ApplicationUser is not part of the model for the current context".
I've can't find any reference to AspNetUsers to that I can substitute for my User table.
Am I forced to modify the existing localdb and rewrite my previous database to work with the new AspNet table structure and if so can the database then be migrated and upscaled to SQL Server 2012?
Any assistance would be much appreciated :-)

Simply enable migrations for your project (assuming VS2013 - newer versions are different and don't require migrations to be enabled for vNext)
1. Enable-Migrations
2. Update-Database -Script -SourceMigration: $InitialDatabase
That will generate the scripts you need.

Solved this by closing Visual Studio 2013 and locating the path of the localdb which was WebApplication1\App_Data, there were two files aspnet-WebApplication1-20140430012043.mdf and aspnet-WebApplication1-20140430012043_log.ldf residing in that folder.
I modified the security permissions of WebApplication1\App_Data so that SQL Server 2012 would have no issues connecting to it.
I then opened Microsoft SQL Server Management Studio and proceeded to attach the database, once the database was attached to the instance, I backed the database up. Next I dropped the database and carried out a restore, this time renaming the database as per the project and relocating the mdf and ldf files.
I then opened my project in Visual Studio 2013 and modified the Web.config from:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20140430012043.mdf;Initial Catalog=aspnet-WebApplication1-20140430012043;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
to:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=MYSERVER;Initial Catalog=WebApplication1;Integrated Security=True" providerName="System.Data.SqlClient" />
Ran the project and all is well.

Related

Database is removed from Microsoft SQL Server, Why?

I created a new Empty website in Visual studio Express 2013.
I created an ASP.NET folder: App_Data.
Then I went to SQL Server Management Studio. I created a new Database in which I provided the path to the App_Data of the website that I created.
I named the database "FacebookData"
Then I went to Visual studio; I found the database in the App_Data folder and also in Server Explorer.
I went to Web.config, And under I added
<connectionStrings>
<add name="DataConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FacebookData.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I click Build -> Rebuild Solution
When I close SQL Server Management Studio, and reopen it again.
The Database named "FacebookData" is missed from the SQL Server Management Studio Databases list!!!.
I still find it in the Visual Studio in App_Data folder and Server Express.
If I close and open the SQL Server Management Studio before adding the to the Web.config. The Database is still exist in the SQL Server Management Studio. which makes me feel that there is something wrong in the connection string.
Any clues?
Your connection string says that database should be "attached" to SQL server when application is run. To have it permanently living in SQl server you should just use SQL server management studio to attach it manually, that way it stays attached. You also need to change you connection string to
"Server=your_sql_server_instance;Database=FacebookData;Integrated Security=True"

How can I transfer my login system from sqlexpress to my own server?

I am fairly new to the login system visual studio comes with and I've been wondering if is possible to transfer all the user information created automatically by visual studio from sqlexpress to my own database?, I've noticed that ASPNETDB.MDF contains the database information created for users and I wish to use it or store it in my own sqlserver instead of sqlexpress.
Ultimately my goal is to change the following connection string to my own connection string containing the name of my own server and database:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
Yes, you can do this. The stock ASP.NET membership tables in your SQLEXPRESS database are generated using the aspnet_regsql.exe tool located in %WINDIR%\Microsoft.Net\Framework\(FrameworkVersion). So you should be able to run this tool on your pointing to your new sql server. Then backup and restore the data from the ASPNETDB database (as long as your sqlexpress and sql server versions match). Here is a good overall article about the generation of the membership tables.

How to copy local Membership and Role database to remote MS SQL Database

I have made a MVC application that uses the built-in ASP.NET login functions. It works perfectly on my local machine. I have bought a webhosting service, because i want to publish my website to the internet.
How do i copy the membership and role database / tables to my new MS SQL Database and make it work with my current project?
In my Web.config i have:
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
notice that
Instance=true
This means that SQL Server Express will open the aspnetdb.mdf file, creating it on the fly first if needed.
When i try to login on my website i get the following error:
Sorry, an error occurred while processing your request.
Thanks
You need to run aspnet_regsql located in C:\Windows\Microsoft.NET\Framework\v4.0.30319. Just point it to your remote sql server and it should take care of the rest.
You will also need to edit your connection string for application services to the one for your remote sql server.

ASP.Net SQL ConnectionStrings config

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
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.
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.
I can never remember connection strings, I use this site to figure out what they need to be.

Database in App_Data folder -> Unable to open the physical file "xyz.mdf"

I am creating a package of an asp.net mvc3 application to distribute it. Means I want to zip it and send it to someone for review. I want that you just start the solution in VS and it runs. So no need for changes in web.config.
I was using sql server with hard-coded DataSource. (COMPUTERNAME\SQLEXPRESS)
I changed it to .\SQLEXPRESS to make it relativ. Better.
Then i detached the db with SSMS, copied the DB to the App_Data folder, added AttachDBFilename=|DataDirectory|MyDatabase.mdf;
And (as found on msdn) Initial Catalog=; "use it but don't set it"->great feature ;)
This is the result:
<connectionStrings>
<add name="DBService" connectionString="Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|MyDataBaseFile.mdf;Initial Catalog=;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
When I start the asp.net Development Server (hit "play") in VS Web Developer 2010 there is a Database error:
Unable to open the physical file ".........mdf". Operating system error 5: "5(Zugriff verweigert)".
Any suggestions how to attach the file to the solution? What about switching to Compact Edition?
Have you tried adding
User Instance=True
?
Data Source=.\SQLEXPRESS;AttachDbFilename="|DataDirectory|MyDataBaseFile.mdf";Integrated Security=True;User Instance=True

Resources