MVC 3 Connection String Confusion - asp.net-mvc

Using VS 2010, MVC 3, and SQL Express, I've been following the tutorials on ASP.net. I'm also using EF code first. So far, my connection string has been:
<add name="MusicStoreEntities"
connectionString="Server=.\SQLExpress;Database=MusicStoreDB;Integrated Security=SSPI;User Instance=true"
providerName="System.Data.SqlClient" />
Which creates the db in:
C:\Documents and Settings\username\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS
I now want to have the database reside in the App_Data folder. So I created a new web application following a different tutorial, but this time I added a SQL database to it, and modified the connection string for the membership database by changing the database name. I then used aspnet_regsql.exe to create the membership tables. I then created POCO classes, a controller and added seed data. Since the database already existed, it complained about not having an EdmMetadata table. Upon researching that I realized my mistake and deleted the database. Not to mention the DB will be dropped and created as the project is modified. Running the project again gave me the error "Initial catalog not specified" so I modified the connection string. Running the project again I get the error 'C:\ContosoUniversity\ContosoUniversity\App_Data\School.mdf' already exists. Choose a different database name.
A search of my hard drive reveals this database does not exist. My connection string looks like this:
<add name="SchoolContext"
connectionString="data source=.\SQLEXPRESS;Initial Catalog=School;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|School.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
What is making it think the DB exists? What do I need to do to get code first to create the DB in the App_Data folder?

Although I ended up changing the database name in frustration, a search of my registry revealed multiple entries for school.mdf in the app_data directory. Removing these solved the problem.

Related

How to use existing SQL database in my separate project under one solution in ASP.NET MVC application

I have created 2 separate projects under one solution where one project is a class library. I have created an ADO.NET Entity Model from my existing database into this. Another project is an ASP.NET MVC web app.
I have used Individual User Authentication and wanted to use registration and login facility provided by this.
Now I am unable to use my existing database as this project is creating new database by itself when first runs.
Please guide me how to use existing database. I have given reference to my ASP.NET MVC project. These are my connection strings. Please help me.
<connectionStrings>
<add name="LAPTOP-71D4QMB6"
connectionString="Data Source=LAPTOP-71D4QMB6;Initial Catalog=VCAdvertiserDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="HBDBEntities"
connectionString="metadata=res://*/Data.HBModel.csdl|res://*/Data.HBModel.ssdl|res://*/Data.HBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=LAPTOP-71D4QMB6;initial catalog=VCAdvertiserDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Error thrown is:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
You have to copy the connection string from the app.config of your class library to the web.config of the MVC project. You need to put it in both the root web.config and also have to put it in the shared web.config under the shared views folder. That last may not be needed but it also doesn't hurt.
That worked for me.
Joey

Deployed application doesn't work after changes connectionString MVC ASP.NET

I made application using DataBase First approach with oracle (in this way https://www.youtube.com/watch?v=tk_EDjTzZCE so I used Entity Wizard)
I deployed my application on http://www.smarterasp.net/
Everything works fine.
But I decided to change connectionString in webconfig. Because I wanted to use database from another server. New Database has literaly same structure as old but still doesn't work.
Also I tried to change connectionString before deploy. I compiled the application and when I run - receives an error: ORA-00942: table or view does not exist
If I add database again using Entity Wizard (check youtube link above) then everything works fine. How can avoid this? Maybe my connectionString is wrong?
<connectionStrings>
<add name="DbEntities" connectionString="metadata=res://*/DAL.DbModel.csdl|res://*/DAL.DbModel.ssdl|res://*/DAL.DbModel.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="DATA SOURCE=some-link.com:1521/ORCLBK;PASSWORD=some_password;USER ID=some_login"" providerName="System.Data.EntityClient" />
</connectionStrings>

MVC project database cant find

I'm a beginner in MVC application development. I'm just trying to make a MVC application getting help from internet. That was a simple example and working properly. The article is said the database is in app_data folder. But actually there is no any database in that folder. Then I just tried to find the physical database location using the connection string.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-ePhoneBook-20140322204146;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
as the above connection string (I just not write down this and it is auto generated code in web.config file), the database name is "aspnet-ePhoneBook-20140322204146". Then I checked weather this DB available in physical location of the sql databases. My all other databases (My old projects' databases) are in that folder. But couldn’t find this database. then I tried with folder searching option in windows 7. There is no any database with that name. The wonder is my application run properly. Data saved properly. SQL management studio also not showing the database.
My question is, How to find the physical location of the database and why is this database not showing in SQL management studio.
The issue is you're pointing to SQL server, and ,definitely, not to separate attached database file.
your connection string should look like this:
<connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ePhoneBook-20140322204146;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ePhoneBook-20140322204146.mdf" />
where |DataDirectory| - is actually App_Data folder in terms of ASP.NET.

sdf file not showing in App_Data (using MVC application)

I followed the "Creating an Entity Framework Data Model for an ASP.NET MVC Application" in ASP.net and changed it to meet my needs.
I built the project and run it - everything works fine (I can view details, edit and save).
I press "Show all files" and there is nothing under App_Data.
I tried to read the related threads here but i couldn't fix it.
I will appriciate any Idea!
this is my connection string:
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
Thank you!
First thing is that your connection string indicates this is a SQLExpress database file. Probably located under the data directory under SQLExpress install location.
Second, I ran a tutorial and chose to use a SQL Compact database. That file (.sdf) I would expect to show up under the App_data folder. It did not. Since the database was generated via 'code first' I suspect this is a bug. All I had to do to fix it (working in the Solution Explorer of VS 2012 RC) was right-click on the App_data folder and choose . And I had to set the .sdf file properties so it would copy to the output file.
I knew I had to do this because my web deploy failed to write the .sdf file to my IIS server. When I republished, the 'Preview' showed me that the .sdf file was going to deployed to /bin/app_data of my web application folder.
If you are using a SQLExpress database, it might require slightly different steps. But your problem is similar and I think we can say this is a bug in the Entity Framework when working within Visual Studio.
To all of those who "tried everything" when it comes to this issue and it still doesn't work, if you can run the app, try saving a record in the database, as the .sdf file will only be created once the first record is created. Then switch off and on again the show all files button and the .sdf file should show up.
Good luck!

Entity Framework 4: The Phantom MDF

Yesterday, I decided to convert my application from LINQ-to-SQL to EF4 to take advantage of the code-first feature. I created my POCOs, a DbContext class, implemented new repositories classes and all was fine.
At some later point, I noticed that there was a typo in the filename EF used to create the SqlExpress MDF file. I moved the old DB out of its folder, cleared connection strings out of my config files, and let EF create a new, empty DB. It created the file, added a connection string to my app.config file, and the application works fine. However, when I look in the folder specified in the connection string, there is no MDF present. In fact, the folder doesn't even exist. Here is the connection string that EF added:
<add name="PhotoAlbum.Infrastructure.Properties.Settings.PhotoAlbumConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Src\Visual Studio 2010\Projects\PhotoAlbum\trunk\PhotoAlbum.Web\App_Data\PhotoAlbum.mdf";Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
Seeing that EF decided to put the file on C: instead of D: (where the folder does exist, but the file doesn't), I changed the connection string to:
<add name="PhotoAlbum.Infrastructure.Properties.Settings.PhotoAlbumConnectionStringg"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="D:\Src\Visual Studio 2010\Projects\PhotoAlbum\trunk\PhotoAlbum.Web\App_Data\PhotoAlbum.mdf";Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
EF did not create a new file, or try to open an existing file, on the D: drive. This apparently makes no difference to EF, because it still is able to access this phantom MDF file.
I've searched my entire computer and there is no PhotoAlbum.mdf file anywhere on my system. Where has this file gone? Why can EF find it, but I can't?
This seems to only have been an issue with the CTP version of EF. The SQL CE files are showing up now.
What did you use to find the file? Is your operating system 64 bit? If it is, then if you are using a 32 bit tool to look into the filesystem, it will show you a virtual view of it.

Resources