sdf file not showing in App_Data (using MVC application) - asp.net-mvc

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!

Related

Execute Code first Migration

I have installed Windows 8.1 pro and moved all MVC projects to new work-space using VS 2013 Community Edition. All projects were using code first migration.
Database Publishing was also done using code first migration. All was running fine before I updated my machine.
But Now when I publish web application to remote servers, 'Publish Web' dialog box not showing "Execute Code first Migration", Instead its showing "Update database" for updating database.
And when I close it, It modifies the publish files for DB First. It seems that some global setting enforcing it to the following code:
<Objects xmlns="">
<ObjectGroup Name="DefaultConnection" Order="1" Enabled="False">
<Destination Path="" />
<Object Type="DbDacFx">
<PreSource Path="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MVC-20140923125211.mdf;Initial Catalog=MVC-20140923125211;Integrated Security=True" includeData="False" />
<Source Path="$(IntermediateOutputPath)AutoScripts\DefaultConnection_IncrementalSchemaOnly.dacpac" dacpacAction="Deploy" />
</Object>
<UpdateFrom Type="Web.Config">
<Source MatchValue="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MVC-20140923125211.mdf;Initial Catalog=aspnet-MVC-20140923125211;Integrated Security=True" MatchAttributes="$(UpdateFromConnectionStringAttributes)" />
</UpdateFrom>
</ObjectGroup>
</Objects>
Its strange why "Execute Code first Migration" is not shown for all my projects.
This behavior is same for all my other projects too.
The App_Data databases also not opening. So I changed connectionString' datasource from Data Source=(LocalDb)\v11.0 to Data Source=(LocalDb)\MSSQLLocalDB (MSSQL Sever 2014 express). Changing DataSource worked for opening database though.
I tried the solutions:
Tried this : "Execute Code First Migrations" checkbox disappeared from my publish profile
Enable Migration -Force
Clean, Rebuild and restart project and
Visual Studio.
Delete and re-import publish file
I tried solutions above few months back. That time the points above worked but now nothing works.
Is there any solution available for this? I wasted my whole day and still not found solution.
You need to install the Visual Studio update 4. Go to Extensions and updates under tools.
You might find this helpful:
Entity Framework Code First Data Migrations not working with VS2012 Web Deploy
I myself am still trying to find a simple way to deal with migrations.
I found a temporary solution for this. Manually replacing all ConnectionString name (Defaultconnection) to context name "ApplicationDbContext" without namespaces solved the problem.

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.

Trouble deploying EF CodeFirst to server

I have a CodeFirst web application that originally used LocalDb. I changed the connection string to:
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SqlExpress;Initial Catalog=aspnet-BatchFindings;Integrated Security=True" />
And I created the appropriate database using Server explorer.
I am using WebDeploy to publish to a Windows Server 2012 instance running IIS8.
After publishing, I was unable to use the application; it did not have permissions for the database. I then changed the identity to a user account, and now I do not get database errors. Except the application does not appear to save any data to the database. I am at a complete loss - any help is appreciated!
Recap:
Entity Framework, Code First
Web Deploy to IIS 8
Custom Application Pool, identity set to a user account
Data not being stored in database.
EDIT: I would love to be able to simply use Web Deploy and have it use its own database (so it does not get test data from mine) but this is just so frustrating! I am publishing, the connection string should work, not sure where I am going wrong.
Stupid question but are you calling DbContext SaveChanges() method after data manipulation - data saving works when testing?
Also you can use different web.config files when testing (Debug) and deploying (Release version).
So I inadvertently figured out the reason why it was not saving. For some reason in my test environment everything worked, but when it was pushed out, I needed to add the following line:
db.Entry(obj).State = System.Data.EntityState.Added;
I checked out the tutorial, which seemed to work until it went into that Grant.sql file. There is no option in Visual Studio to connect.
What I ended up doing in that case to get the database working was set my Application Pool to run under a user identity I created for this purpose, while setting Load User Profile to True. You can also run as LocalSystem (as long as you create the following folder: C:\Windows\SysWOW64\config\systemprofile\Desktop - this tricks the system into thinking the LocalSystem account is interactive).

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.

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