LocalDb error after scaffolding - asp.net-mvc

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>

Related

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.

Connection string EF Code First

I started an application using Entity Framework Code First.
In my Web.config I added the connection string:
<add name="MyContext" connectionString="Data Source=server\mssqlserver2008;Initial Catalog=dbname;persist security info=True; Integrated Security=SSPI" providerName="System.Data.SqlClient" />
And I received a error when I tried to access my Controller: CREATE DATABASE permission denied in database 'master'
So, I debugged the code and I found that the attribute "ConnectionString" inside my Context it's different from my Web.config:
Data Source=.\\SQLEXPRESS;Initial Catalog=MyProject.Models.MyContext;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE
Why the ConnectionString is wrong??
In your EF initialization code, make sure you specify the connection string name like this
public class MyDbContext : DbContext
{
public MyDbContext() : base("MyContext") {}
}

I cannot locate where mvc4 store its database

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);
}

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

Resources