Entity Framework 4: The Phantom MDF - entity-framework-4

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.

Related

how to create tables on azure and connect them with VS2013

I have built my MVC 5 project with a database using entity framework db first. I published my website but had problems with my database.after lots of research to solve the problem,i figured to build my database on azure since it's only 2 tables and copied the connection string to the web.config file on my MVC 5 project.
when publishing again with the new connection string update, an error showed indicating that can't find the database:
The system cannot find the file specified
Exception Details: System.ComponentModel.Win32Exception: The system cannot find the file specified
This is what the azure offered me as a connection String:
Server=tcp:idud662mdp.database.windows.net,1433;Database=sportsMArAiCkhuw;User ID=sportsMania#idud662mdp;Password={your_password_here};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;
I have modified it and this is my connectionString:
<add name="DataEntryNew" connectionString ="Data Source=idud662mdp.database.windows.net;Database=sportsMArAiCkhuw;User ID=sportsMania#idud662mdp;Password=My-Pass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient"/>
What's going wrong?
Your connection string is not the same one as provided by Azure. Try changing the DataEntryNew tag in the web.config file to:
<add name="DataEntryNew" connectionString ="Server=tcp:idud662mdp.database.windows.net,1433;Database=sportsMArAiCkhuw;User ID=sportsMania#idud662mdp;Password={your_password_here};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient"/>

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.

MVC 3 Connection String Confusion

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.

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!

Where to locate the App_Data folder in an MVC solution divided in 3 projects?

I have a solution divided in 3 projects:
Domain
Web User Interface
Unit tests
In my web.config, I defined my database like this:
<connectionStrings>
<add name="EntityFrameworkDbContext"
connectionString="Data Source=.\SQL2008;Initial Catalog=MvcLearning;Integrated Security=SSPI;Database=MVCLearning;AttachDBFilename=|DataDirectory|MVCLearning.mdf;MultipleActiveResultSets=True;User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
We have the |DataDirectory| used in my connectionstring.
Everything about the database should be located in the 'domain' projet, right? But when executing the solution for the first time, the database is created and I see that it is created in the WebUI\App_Data folder...
I thought it should be located in the App_Data in the 'domain' projet, shouldn't?
Thanks.
Everything about the database should be located in the 'domain' projet, right?
Ideally it should be located in a data access layer which should be a separate layer of your application than your business/domain layer.
I thought it should be located in the App_Data in the 'domain' projet, shouldn't?
No, since the hosting project is the ASP.NET MVC application, things like App_Data are special folders which have only sense in a web application. So when you define |DataDirectory|MVCLearning.mdf in the web.config of your web application that really means the App_Data folder of the web application.
In domain project you have your business logic and entities. But if you use |DataDirectory| it is placed in App_Data. Your domain project has nothing to do with where data file is stored.

Resources