I cannot locate where mvc4 store its database - asp.net-mvc

I am very new to MVC4 and try to learn everything by doing a simple project. I created a model and a control succesfully. But I couldnt understand where it stores its database.
I can do insert,delete,update records on webpage created by ASP.NET, but I cannot locate where that database is. I checked the web.config but there is no connection string. how can I find out where the system stores its data?
I have sql-server installed on my computer but I checked it and it is not stored there

MVC4 stores the database in an mdf file located in your App_Data folder. The connection string is available in the top-level web.config.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication6-20130610160316;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication6-20130610160316.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>

Global.asax.cs:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Use LocalDB for Entity Framework by default
Database.DefaultConnectionFactory = new SqlConnectionFactory(#"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}

Related

LocalDb error after scaffolding

I just started with asp.net mvc, I used the scaffolding generator to create a controller.
My model is called: Person, I set up the DbContext class correctly.
It did generate all the views and the controller actions but it throws an exception and points me to:
// GET: /Person/
public ActionResult Index() {
return View(db.Persons.ToList()); //this line is highlighted as error
}
I browsed a few questions on SO and google and it basically says that I have to setup a localDB in order to get rid of that error.
If the resources are right, the localDb should be located at the App_Data folder right? Mine is empty ...
Isn't Visual Studio 2012 supposted to set that up for me ?
This is the error message:
provider: SQL Network Interfaces, error: 26
Should I create the localDb file manually?
Any suggestions?
edit:
This is my connectionString (Web.config)
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-PersonDb-20160108102518;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-PersonDb-20160108102518.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>

Where is my MVC 5 code-first database created? (not using AttachDbFilename)

I have the following connection string in my MVC 5 weApp:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;User Instance=True;Initial Catalog=MatWebDB;Integrated Security=True" providerName="System.Data.SqlClient" />
I'm using Entity Framework code-first and Identity 2 for forms authentication.
Evreything seems to work fine: i register a new user and it logs in properly.
The problem is that i cannot find the database that was created; in my SQL Server Management Studio there is no "MatWebDB" database created.
What i want to do is to use the same database generated automatically by the Identity 2 authentication, for my own POCO entity objects. And know where is it created.
Thanks!
The easiest way to combine them is to have your application context inherit from the identity context and set the connection string there:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
static ApplicationDbContext()
{
// Set the database intializer which is run once during application start
// Use an initializer that will create the database, or create it in SSMS first
Database.SetInitializer(new CreateDatabaseIfNotExists<ApplicationDbContext>());
}
// Your identity and application tables will be created in the database this connect string points to
public ApplicationDbContext()
: base("DefaultConnection", false)
{ }
// define your POCOs here
public virtual DbSet<Foo1> Foo1s { get; set; }
public virtual DbSet<Foo2> Foo2s { get; set; }
.. etc
}
Very simply. In your Web.config
<configuration>
<connectionStrings>
Using code first and Sqlserver:
If you have a local database you use a connection string like so:
<add name="DefaultConnection1" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;
AttachDbFilename=|DataDirectory|\aspnet-WebApplicationMVC-xxxx.mdf;
Initial Catalog=aspnet-WebApplicationMVC-xxxxx;Integrated Security=True"
providerName="System.Data.SqlClient" />
To see the database in visual studio:
If you have a database hosted on an online server:
<add name="DefaultConnection" providerName="System.Data.SqlClient"
connectionString="Data Source=SQLxxxxx;integrated security=false;
Initial Catalog=DB_9D914B_kermit;User Id=DBxxxxx;Password=xxxx;
Trusted_Connection=false;TrustServerCertificate=false;Encrypt=False;
MultipleActiveResultSets=True" />
To see the database in SQL server management studio:
The security attributes of the connection strings can be varied to suit your needs.
Step 1:
In Solution Explorer, there is a default folder named "App_Data" firstly, just click to select this folder.
Step 2:
Now click the above menu bar icon i.e "show all" as shown in the below image.
You will find your db there, just double click on db file,for editing or add new tables.

Unable to access data in MVC using Entity Framework

I have a table named Employee in my database PragimTech, and two classes Employee and EmployeeContext in my Models folder.
My connection string in web.config is as follows:
<connectionStrings>
<add name="EmployeeContext" connectionString="data source=GAURAV-PC/SQLEXPRESS; initial catalog=PragimTech; integrated security=SSPI"
providerName="System.Data.SqlClient"/>
When I run my application, i recieve a ProviderIncompatibleException saying
The provider did not return a ProviderManifestToken string
I am unable to find the bug. Please help.
This topic could be helpfull for you.
Check your InnerException for more details about error.

Splitting an Solution into multiple projects

I created a Black Solution (Arhi) with a ASP.NET MVC 4 Internet project (Arhi.Core) and Class library project for data (Arhi.Data) where I'm storing my EDMX Model.
I added a reference for Arhi.Data into Arhi.Core and I tried to add a Controller with a Model class from Arhi.Data (People entity) and I got this error.
'Unable to retrive metadata for 'Arhi.Core.People'. The specified
named connection is either not found in the configuration, not
inteneded to be used with the EntityClient provider, or not valid.'
Q : Why did I get this error ? Is my approach wrong / any recommendations?
Q2 : If I want to add RDLC reports to my solution, should I also use a Class Library project ?
Connection string from Arhi.Core
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-SalvamontMVC-20121108140556;Integrated Security=SSPI" />
and Arhi.Data
<add name="SalvamontEntities" connectionString="metadata=res://*/ModelSalva.csdl|res://*/ModelSalva.ssdl|res://*/ModelSalva.msl;provider=System.Data.SqlClient;provider connection string="data source=www.arhimedes.ro,1433;initial catalog=Salvamont;persist security info=True;user id=sa;password=********;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
in your first connection string not have user name and password if two database deference then give user name and password in first connection string

MVC Entity Framework connection string references other project

I'm doing a Model first approach for a Microsoft MVC application. The solution is named "TutorialPile" divided into two projects, Domain and WebUI. I try to add a controller for the Tutorial object to the WebUI project, and I select the domain class and the DB context. However, I get the error, "Unable to retrieve metadata for TutorialPile.Tutorial. Unable to load the specified metadata resource."
Looking around online it looks like it can't find the edmx object in the connection string in the web.config file. I copied the connection string from the Domain project's app.config file but it still doesn't work. Here are the connection strings from the web.config file.
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="TutorialPileModelContainer" connectionString="metadata=res://*/Models.TutorialPileModel.csdl|res://*/Models.TutorialPileModel.ssdl|res://*/Models.TutorialPileModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=TutorialPileDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="TutorialPileDbContext" connectionString="metadata=res://*/Models.TutorialPileModel.csdl|res://*/Models.TutorialPileModel.ssdl|res://*/Models.TutorialPileModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=TutorialPileDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
And here is the connection string that I copied.
<add name="TutorialPileDBEntities" connectionString="metadata=res://*/TutorialPile.csdl|res://*/TutorialPile.ssdl|res://*/TutorialPile.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=TutorialPileDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Any ideas on what I need to change?
Make sure...
the edmx file's build action is set to EntityDeploy (select the file and go to the properties window).
Your WebUI project references the Domain project
Edit the connection string on your WebUI project to reference the metadata from the other project. Using * means it can come from any dll. BUT, if your edmx file is inside a folder, you have to map the hierarchy.
Eg:
Path to EDMX: TutorialPile.Domain/Model/TutorialPile.edmx
Connection string: res://*/Model.TutorialPile.csdl|res://*/Model.TutorialPile.ssdl|res://*/Model.TutorialPile.msl
Even better:
The documentation suggests you specify the assembly (for performance reasons) using the full name of the assembly (something like: AdventureWorks, 1.0.0.0, neutral, a14f3033def15840). I couldn't make it work. But, using only the name of the assembly works for me. So, if your domain project outputs a TutotialPile.Domain.dll, you can use:
res://TutorialPile.Domain/Model.TutorialPile.csdl|res://TutorialPile.Domain/Model.TutorialPile.ssdl|res://TutorialPile.Domain/Model.TutorialPile.msl

Resources