Entity Framework Does Not Create Database in App_Data - asp.net

i am working on a web api and i need to have a local db in the App_Data folder, I'm using Entity Framework Code First but it does not create database in the App_Data
Here is my connection string :
<connectionStrings>
<add name="TestDBContext"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=TestDB;Integrated Security=True;MultipleActiveResultSets=True;AttachDBFilename=|DataDirectory|TestDB.mdf;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
i used show all file in the project menu and bla bla bla
but database is being create in
C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA
how do i fix this?

Yes, that is the default. Two ways to fix:
1) Add connection string to constructor of your context:
public ApplicationDbContext()
: base("TestDBContext") { }
2) Add a default connection factory to your config:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=TestDB;Integrated Security=True;MultipleActiveResultSets=True;AttachDBFilename=|DataDirectory|TestDB.mdf;User Instance=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
See https://msdn.microsoft.com/en-us/data/jj556606

Typically you need to change the settings in the app config to be 'AttachDbFileName' else it defaults to a SQL Server Express instance. Usually you can just set the mode to attached and fix this:
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
Here is an MS page on connections for you too:
https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx#sqlce\
Although I would SERIOUSLY suggest if this is not a small compact app that will have small growth you do not do an attached database. This is just a bad idea for deployments of large scale apps and a bad practice in general unless you know your database will mostly always be small. Just my two cents though.

Problem solved by installing Sql LocalDB

Related

asp.net mvc Invalid value for key 'attachdbfilename'

I am currently reading Manning's "ASP.NET MVC 4 in Action" book and trying to get the first example to work.
Within my test application, I built a simple Model and created some Views. I then imported "SQL Server Compact" using NuGet.
When I finally try to run the application I get the following error:
Invalid value for key 'attachdbfilename'
This occurs on every interaction with the Database (SELECT or other CRUD operations) I am running. Any ideas?
Though, I am bit late to respond to this question but I was facing the same issue and I resolved it by modifying the connection string like below
<add name="MovieDBContext"
connectionString="Data Source=.;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
Hope this would be helpful for others too.
I tried finding a solution for this particular error without success.
Fixed it in the end by updating the .Net Framework 4 to 4.0.2. Patch and details can be found Here is the link
Hope it helps
I think the problems here is string in web.config is not correct
According to your set up sql, the string will work if you set something similar like this
<add name="MovieDBContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
Data Source=.\SQLEXPRESS; --> sometime, it will be Data Source=.;
after that, you need config right to access FOlder App_Data
If u test on Window 7, right click folder. property
Security tab -> add user network Service with full right
Go to section of Web.config and modify the section to the following to get SQLServerCE4 to be used:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0"/>
</parameters>
</defaultConnectionFactory>
</entityFramework>
I guess problem occurs only when using SQL Express on .NET Framework v4.0.30319 anyways
SQL Server Express Local Database Runtime support in SqlClient is added in .NET Framework update 4.0.2 read - http://support.microsoft.com/kb/2544514
Alternately use connection string similar to this
<add name="EmployeeDbContext" connectionString="Data Source=.;Initial Catalog=Employees;AttachDBFileName=|DataDirectory|\Employees.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
above connection string was giving an error but as soon as added"\" before the name of my database the error got resolved.
Try this:
<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
You are using SQL Server Compact 4.0:
Be sure you have installed SQL Server Compact 4.0.
Install VS 2010 SP1 Tools for SQL Server Compact.
Change your connection string to:
<connectionStrings>
<add name="MyModelDBContext" connectionString="Data Source=|DataDirectory|mymodel.sdf" providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Right click on controllers folder and -> add -> controller
Type:
YourController
MVC-Controller with read/write ...
MyModel
new datacontext -> APP.Models.DatabaseContext
Here is a blog post I found about that error. It is a bit old, but uses the express version of sql server. MSDN Link
That blog post talks about expecting the server name of the sql database to be a local address and not /sqlexpress
There is also this blog post that talks about having an incorrect connection string. So maybe check your connection string to the database. and your problem could be there.
Facing the same issue, I have looked over my connection string and found that "UNC-style" paths e.g. \\MyServer\MyDBServer are not allowed, one has to use the MyServer\MyDBServer syntax for a remote host or the .\MyDBServer syntax for locally hosted DB Servers.

Is it recommended to use seperate database for forms authentication for MVC4 project?

One database for the project and once for the authentication created by the MVC4 template.
Reply to Mystere Man:
I was following this MVC4 example:
It creates two connection strings in web.config file, one for forms authentication and the other one for product database.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ProductStore-20120829112625;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ProductStore-20120829112625.mdf"
providerName="System.Data.SqlClient" />
<add name="OrdersContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=OrdersContext-20120829131625; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|OrdersContext-20120829131625.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
No. It's not recommended, but it's not "not reccomended" either. You can put your membership tables anywhere you want. What's recommended is to put them where you want them.
But frankly, I see no valid reason to use a separate database, other than personal preference. I personally think it's pointless to put them somewhere else. Now you have to backup two databases and keep the backups synchronized.

EF Code First with SQL Server Express 2012 ConnectionString

Everything works just fine with the following connection string.
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
I recently installed SQL Server 2012 Express on my local machine to test with, but I cannot make the connection. Here is my connection string using Windows Authentication.
<add name="ApplicationServices" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
I'm a total noob who tried his best to search the forums, but am unable to defer my solution from the 'Questions with similar titles' section. Any help is greatly appreciated.
EntityFramework code first uses a defaultConnectionFactory in the app.config or the web.config file which will automatically connect to .\SQLEXPRESS. This configuration looks like this:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
</entityFramework>
If you're using EF Code First, you probably have a class derived from DbContext to use as context. By design, EntityFramework will look for a connection string having the same name as your context class and use that one instead of the defaultConnectionFactory. This is how I'm using it:
<connectionStrings>
<add name="ObjectContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=My.Db.Name; Integrated Security=True; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
What I'm suggesting is to check that EF doesn't use it's defaultConnectionFactory and even force overriding that by adding the connection string named after your context.
You should also check this blog article which gives great details about the EF configuration file.
Are you sure this part of the connection is right?
"Data Source=.\SQLEXPRESS [Name];
I've not used SQL Server 2012, but try to remove the [Name] text.
This worked for me
add name="MovieDBContext" connectionString="Data Source=.\SQLEXPRESS;
Initial Catalog=Movies;
Integrated Security="True" providerName="System.Data.SqlClient"
I was working through this tutorial and had to work it out. The MovieDBContext is created in code in the model of an mvc application
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
This automates the IDE creating the controller class but it wouldn't work till I got the connection string right in the web.config file.

ASP.NET Sql Provider

I got a problem. When i'm creating the new project in ASP.NET using VS 2010, it's web.config with a default connection string about SQL Express created. But i haven't even got SQL Express installed. What should i do to change the default AspNetSqlProvider to work with my instance of full-weight SQL Server as a services database? And how can i change the template for the ASP.NET project to create a project with my connection?
You mean this one:
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
To change this into one that'll work with SQL Server, it needs to look more like this:
<add name="ApplicationServices"
connectionString="data source=ServerName;Initial Catalog=DatabaseName"
providerName="System.Data.SqlClient" />
where DatabaseName is probably aspnetdb. It'll need some form of authentication, whether you use Windows authentication (in which case you can copy the Integrated Security element from the original connection string) or SQL Server authentication (where you'll have a username/password combination).
There's great info on building connection strings at connectionstrings.com.
To fix this for your future projects, you'll need to change the template for Web Projects. Locate the folder where your C# Web templates are kept (for me this is C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\CSharp\Web\1033.
Take a copy of the WebApplicationProject40.zip file (you might also want to backup the original somewhere!).
Inside it you'll find the web.config file with the SQL Express connection string. Change it to a SQL Server string, and then re-assemble the zip file.
The last step is to rebuild the VS template cache - from a command-line (VS Command Prompt probably works best), run devenv /installvstemplates. See here for details.
I just had a similar problem. I had a web site created in VS 2008 and wanted to try web parts. Added some to a page, then started getting:
SQLExpress database file auto-creation error:
The connection string specifies a local Sql Server Express instance using a database >location within the applications App_Data directory.
I ran aspnet_regsql.exe (wizard, in .Net framework folder) to create the database and then I added a connection string to web.config:
<configuration>
<connectionStrings>
<add name="aspnet_membership" connectionString="Data Source=localhost; Initial Catalog=aspnetdb; Integrated Security=SSPI;"/>
</connectionStrings>
</configuration>
Then I added this to web.config:
<configuration>
<system.web>
<webParts>
<personalization defaultProvider="SqlPersonalizationProvider">
<providers>
<add name="SqlPersonalizationProvider"
type="System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider"
connectionStringName="aspnet_membership"
applicationName="/" />
</providers>
<authorization>
<deny users="*" verbs="enterSharedScope" />
<allow users="*" verbs="modifyState" />
</authorization>
</personalization>
</webParts>
</system.web>
</configuration>

Godaddy ASP.NET membership database woes

I purchased a Windows shared hosting account on godaddy that came with 2 MSSQL databases. I setup one to hold my site data and the other installed aspnet membership schema to store site members. The site works perfectly even displaying data from the 1st database. However when I try to login or register I get this nasty error
Exception Details:
System.Configuration.Provider.ProviderException:
The SSE Provider did not find the
database file specified in the
connection string. At the configured
trust level (below High trust level),
the SSE provider can not automatically
create the database file.
Ive gone through my web.config and there's nothing wrong with my 2 connection strings. It seems godaddy has a problem with using 2 mssql databases simultaneously when 1 is for membership.
Does anyone know a solution or a workaround?
I hope my experience benefits every one. Basically if you want to avoid aspnet membership problems on godaddy always use "LocalSqlServer" as the connectionstring. i.e
<providers>
<remove name="AspNetSqlMembershipProvider" />
<add name="AspNetSqlMembershipProvider" connectionStringName="LocalSqlServer"
..other attributes here... />
</providers>
Then create the "LocalSqlServer" connectionString...remember to remove it first
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="Data Source=xxxx; Initial Catalog=xxx; User ID=xxx; Password=xxx;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I ran into same problem and am using MVC3. Above solution works but with some other changes in MVC3. It took me long time to figure it out so if anybody has similar issue in MVC3 it might help them:
Search for "connectionStringName" in web.config and replace the name with connectionStringName="LocalSqlServer"
Also under connectionstrings make sure
-To add (As this is important for all who are using shared hosting it will replace machine.config LocalSqlServer connectionstring with yours.)
-Keep your current connectionstring (In my case it is FilmiDb, this is required for you to connect to the database in EF model. Without this you will get errors.)
<connectionStrings>
<remove name ="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=.\SQLExpress;Initial Catalog=SofilmiDb;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="FilmiDb" connectionString="Data Source=.\SQLExpress;Initial Catalog=FilmiDb;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>

Resources