So I'm trying to connect to a database using the following in my web.config
<connectionStrings>
<add name="StoreEntities"
connectionString="Data source=localhost;Initial Catalog=testdb;Integrated Security=True" <providerName="System.Data.SqlClient" />
</connectionStrings>
I have a StoreController class:
public class StoreController : Controller
{
StoreEntities storeDB = new StoreEntities();
// Index does nothing atm
public ActionResult Index()
{
return View(categories);
}
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Product product = storeDB.Products.Find(id);
return View(product.Title);
}
}
And also StoreEntities:
using System.Data.Entity;
namespace Assignment_2.Models
{
public class StoreEntities : DbContext
{
public StoreEntities(): base("StoreEntities")
{
}
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}
}
But when I try and visit localhost:myportnumber/store/details/1 it says object reference not set to an instance of an object which I believe means that the app doesn't know about the database. Does the app automatically load the database into objects on startup? Thanks.