I'll explain here a little bit more in English, so anyone can understand. Hope this helps anyone out there This occurs because Visual Studio fails to connect to the database model .
This happens when you change the name and/or the path in the class that extends DbContext and didn't change it in the Web.config file (at the outermost part of your project: the root).
Example:
Imagine you scaffolded the DbContext code:
a) You right clicked a folder in your project and added a "ADO.NET Entity Data Model", and you named it "Model1"
You get the following code:
public class Model1 : DbContext
{
// Your context has been configured to use a 'Model1' connection string from your application's
// configuration file (App.config or Web.config). By default, this connection string targets the
// 'Skelleton.Models.Model1' database on your LocalDb instance.
//
// If you wish to target a different database and/or database provider, modify the 'Model1'
// connection string in the application configuration file.
public Model1()
: base("name=Model1")
{
}
// Add a DbSet for each entity type that you want to include in your model. For more information
// on configuring and using a Code First model, see http://go.microsoft.com/fwlink/?LinkId=390109.
// public virtual DbSet<MyEntity> MyEntities { get; set; }
}
b) Now, you have decided that the name you've just written is plain bad, so you change it to AppContext
Your code now looks like this:
public class AppContext : DbContext
{
// Your context has been configured to use a 'AppContext' connection string from your application's
// configuration file (App.config or Web.config). By default, this connection string targets the
// 'Skelleton.Models.AppContext' database on your LocalDb instance.
//
// If you wish to target a different database and/or database provider, modify the 'AppContext'
// connection string in the application configuration file.
public AppContext()
: base("name=AppContext")
{
}
// Add a DbSet for each entity type that you want to include in your model. For more information
// on configuring and using a Code First model, see http://go.microsoft.com/fwlink/?LinkId=390109.
// public virtual DbSet<MyEntity> MyEntities { get; set; }
}
Then, you try to Scaffold the CRUD (Create, Read, Update, Delete) operations with views and it fails!
Why is that?
Well, if we go to the web.config file, we can see the following string:
<add name="Model1" connectionString="data source=(LocalDb)\v11.0;initial catalog=Skelleton.Models.Model1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
(This line is usually below <add name="DefaultConnection"
)
And there is where the problem lies. You need to change Model1 for the name you've given out!
In this case, it should say "AppContext" instead of "Model1"
And where it says:
initial catalog=Skelleton.Models.Model1;
Verify that:
It's the name of the .cs file that has the class
The namespace (or the series of names (dot-separated) that comes before the name of your class) is the correct one. It's important to notice that you do not append to the end the ".cs" extension; just the name of your file.
It should look like this:
Because I changed the name of the class, both internally and externally (inside it and it's file name), and did not change its location, I just rename it to AppContext
After this has been done. You can scaffold normally ;)
Hope this helps!