I'm trying to add more tables to DefaultConnection database in my MVC project, using code first approach.
I have the following setup:
MyClass.cs
public class MyClass
{
public int Id { get; set; }
public string Name{ get; set; }
public int Age{ get; set; }
}
MyClassController.cs
public class MyClassController : Controller
{
public MyClassDbContext myClassDbContext = new MyClassDbContext();
public ActionResult Index()
{
return View(myClassDbContext.MyClasses.ToList());
}
}
MyClassDbContext.cs
public class MyClassDbContext: DbContext
{
public DbSet<MyClass> MyClasses { get; set; }
}
I have not changed ConnectionString from its default settings since it is the same database that works with user registration and authentication and I have no issues with it.
After running code I get A network related or instance-specific error occurred while establishing a connection to SQL Server... error on return View(myClassDbContext.MyClasses.ToList()).
Question:
Am I missing something basic?
this.Database.Exists()line and see if the DB exist there. If the error says "Server not found", you need to configure SQL Server connection string on web.config file.