Deleting database causes exception until I rename catalog in Web.config - asp.net-mvc-4

If I delete my aspet-*.mdf file and then try re-running my project I get this exception
An exception occurred while initializing the database. See the InnerException for details.
the first time it tries to hit the DB. If I then go into my main Web.config file and change the "Initial Catalog" bit to a different name and re-run the project, it runs fine.
Why? I'm guessing it's caching something somewhere and it thinks that catalog still exists, but it can't find it?
But I don't have to change the AttachDBFilename -- that filename can be exactly the same and it will recreate it as long as I change the catalog.
What is exactly is a "catalog" and how do I fix this problem so that I don't have to keep modifying my connection string?
The full string I'm using is
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Sharpix-20120918b;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Sharpix-20120907.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
Which is only slightly modified from the one that came with MVC4 application.

Assuming your connection string is similar to below
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-123123;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-123123.mdf"
1) Using SQL management studio log on to the data source
Server Type: Database Engine
Server Name: (LocalDb)\v11.0
Authentication: Windows Authentication
2) Once this opens up you will see all the databases that seemingly are being mysteriously created.
3) Right click on the database you want deleted and follow the dialog windows.
The localDB engine, new vof VS 2012, stores all of these development databases. In case you physically delete the .mdf, right click on the database name and delete the database anyway. Ignore the warning message, and then refresh the Databases. The database reference should be gone.
You can do something similar through VS 2012 by selecting View, Server Explorer and then selecting the SQL Server Object Explorer selection from the Server Explorer pane.

Related

How do I construct the initial ConnectionString for EF 6.0 and above?

VS2013 update 3, EF 6.1 using Code First, VB
I have both an MVC5 project and a Forms project in place to learn how to effectively use EF 6. Both projects work fine in my local environment and both default to use my .\SQLEXPRESS server. I notice these initial template projects do not have a ConnectionString in their ~.config files. However, the MVC5 project has a defaultConnectionFactory element where the Forms project has nothing related to EF.
1) How are these projects establishing their connection to the SQL server?
2) If I want to connect to a different SQL server do I simply add a ConnectionString element to the ~.config file?
Assuming the answer to (2) above is correct, I would like to first create the ConnectionString to specify exactly the same connection that is currently in use so I have a known spot to work from.
I found what should be valid connection string constructions in Server Explorer, so I have the details of the 'Data Source', 'Initial Catalog', and the 'Integrated Security' flag. What I don't understand is what to use for the name element of the ConnectionString and if that will maintain the applications' connections to the local .\SQLEXPRESS server.
I hope this all made sense...
Thanks.
Best Regards,
Alan
This was the web.config code I needed to duplicate what was automatically done by the defaultConnectionFactory element:
<connectionStrings>
<add name="BlogContext"
providerName="System.Data.SQLClient"
connectionString="Data Source=<my pc name here>\SQLEXPRESS;Initial Catalog=EFmigration.BlogContext;Integrated Security=True;" />
</connectionStrings>
The important points for me were:
1) The name element is simply the same as the Context name
2) You can't forget the profiderName attribute, but note this thread
From this string I can now apply the web.config transformation elements.
Best Regards,
Alan

MVC 4 Connection String

I everyone,
Lately im learning the MVC 4 and need someone who could explain to me how the connection string work, because I started to create new records in the application but when i access the database(microsoft sql server 2012), the new records arent shown but they are on the application. I've check the app_data but there isnt any mdf file.
Here is the connectino String:
<add name="Tusofona" connectionString="Server=Macieira-PC;Database=master;User Id=sa;Password=nippon;" providerName="System.Data.SqlClient" />
Your connection string looks fine - but it's pointing to a real instance of SQL Server, not a standalone file in App_Data, so make sure you're looking at your actual database.
You're pointed to master though, which is a bad idea. You should create a new database, and point to that one instead.

Where does ASP.NET MVC 4 stores accounts information?

I have installed MVC 4 and created a new "Internet Application". After running the application and adding some accounts I can't find where the information are stored. I have worked with MVC 2 previousely. It stores accounts in an ASP.NET database.
If you are talking about the default settings, it is most likely storing the data in an express instance. In Solution Explorer, view hidden files and you should see it. Otherwise, Use SQL Server to check the database it is connecting to in the connection string.
It's not shown in Solution Explorer.
A brand new MVC4 app creates tables user data in the database that your default connection string points to. So if a new MVC 4 app is created with the connection string:
<add name="DefaultConnection" connectionString="Data Source=localhost\SQLEXPRESS;Integrated Security=SSPI;database=aspnetdb" providerName="System.Data.SqlClient" />
And then you click on "register" in the new app, the result is that the following tables are created and an entry appears in the webpages_Membership table as this screen shot shows:

Few simple questions on managing different types of databases in Visual Studio SQL manager

Recently started with ASP.NET and MVC and have a few questions on working with databases.
I have the following connection string:
<add name="EFDbContext" connectionString="data source=.\SQLEXPRESS;InitialCatalog=EFDbContext;MultipleActiveResultSets=True;IntegratedSecurity=SSPI;" providerName="System.Data.SqlClient" />
And a class (EFDbContextInitializer) that derives from another class that implements the IDatbaseInitializer interface which creates the database if database does not exist. However when it's created it works and all and the database
is obviously created somewhere. However I cannot see it in my SQL
manager in Visual Studio. The database is probably mounted with the built in ./SQLEXPESS so I
figure I should be able to access it somehow when application is not running ? Where is the thing?
Also I have another existing database an *.mdf type that I want to
transfer a bunch of tables to from the above database that
(EFDbContextInitializer) created. The tables I want to export are for the ASPNET
membership provider implementation . I actually used the *.mdf database at first with my
application, but then decided to exclude it from the project so that a
new database could be created with the proper tables for the
membership provider as I don't think it's possible or practical to
try and add those tables to an existing database using the the
(EFDbContextInitializer) way and now I don't want to recreate all the
data in my *.mdf database that I excluded from the project or manually put anything into seed() method. Just want to use my *.mdf database again and add the membership provider tables to it that were created by the (EFDbContextInitializer) in that illusive database.
What is the best tool or method to go about transferring those tables
to the *.mdf database? If I gotta write lots of T-SQL queries please point to some examples. I probably don't know how to use it but I don't think there's
anyway this can be done in the SQL manager in Visual Studio? I know there's compare database
option where you can get the target to match the source, or just get certain tables but once
again I don't know where to find that database that was created automatically. Also should I look
into installing some SQL database management tool? I am using Windows 7 Pro so I take it I can't just
install a full blown SQL server 2008 R2 suite and use the management studio there so what other tools are available to me for proper db management.
AD1. Sure you can connect to these DB's - just connect to .\SQLEXPRESS instance of you local MS SQL Express server using Windows Authentication.
Optionally you can check if the database was created for you on the file system by going to :
C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA
AD2. To add Application Services structures (tables,stored procedures, views) you can use aspnet_regsql.exe GUI tool if you are using asp.net 4.0 it's in
c:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regsql.exe
This will recreate the structure for you. Othwerwise if you would like to keep using your .mdf database for Application Services puproses you only need to add connection string entry in your Web.config. Something like the below should work for you
<add name="ApplicationServices"
connectionString="data source=|DataDirectory|aspnetdb.mdf;;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
You should be able to point SQL management studio to .\sqlexpress and see your database
To transfer data you can use the database Publishing wizard. Right click on your database in the server explorer view in visual studio and you should be able to publish to a provider which will generate your scripts etc for you.

Entity Framework error connecting to SQL Server Express after mounting/connecting in Visual Studio

Just in case I am doing something wrong, I have uploaded a video so you can see every step I have done! I am able to reproduce this error.
Video Link
Basically - Start a new MVC 3 project and using EF in the normal way on a simple class.
I used the following connection string:
<add name="databaseconnection" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|database.mdf;User Instance=true;Initial Catalog=database" providerName="System.Data.SqlClient" />
When I navigate to the site, Everything works ok and as expected.
Now, if I try to double click the .mdf database to open it inside of Visual Studio, all hell breaks loose!
I can connect and view the tables as expected, but even if I do nothing and just close (and/or) delete the connection, next time I go to the application, I get the following error:
One or more files do not match the
primary file of the database. If you
are attempting to attach a database,
retry the operation with the correct
files. If this is an existing
database, the file may be corrupted
and should be restored from a backup.
Cannot open database "database"
requested by the login. The login
failed. Login failed for user
'WIL2-8EEA651803\Administrator'. Log
file 'c:\documents and
settings\administrator\my
documents\visual studio
2010\Projects\TestingEF\TestingEF\App_Data\database.ldf'
does not match the primary file. It
may be from a different database or
the log may have been rebuilt
previously.
No matter what I try, I can't reconnect - the only thing I can do is to rename the database and initial catalog. I am sure that the previous version of the MVC Movie Database Tutorial (before they updated to using compact framework) worked fine like this, so I am unsure why I am having these issues.
So, my question is, what is causing this and what should I be doing instead?
Next, Error number 1 in the video, this is a quick XP VM I use for testing, and it happens at random, but did not happen this time and has never happened on the main machine - so I am curious, but not that bothered. When attempting to build the Controller, I get the following error:
"Unable to retrieve metadata for 'TestingEF.Models.blabla'. The provider did not return a ProviderManifestToken string."
Any idea what this is?
I know for the second issue I had to manully drop the database to get this to go away. I think it has to do with the model and the metadata for the model not being in sync int he database.