I am getting the exception above when I run an application. The application is using asp.net mvc 3 / C#. I made an mdf file and added it under App_Data folder in Visual Web Developer Express. I added connection strings to the web.config folder but when I run and browse to /store, I get the error above with the line var categories = storeDB.Categories.ToList();
highlighted. My database contains 6 tables and one of them is Category.
Controller:
EventCalendarEntities storeDB = new EventCalendarEntities();
public ActionResult Index()
{
var categories = storeDB.Category.ToList();
return View(categories);
}
Connection strings in web.config file:
<connectionStrings>
<add name="EventCalendarEntities"
connectionString="data source=.\SQLEXPRESS;
Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\MvcEventCalendar.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
This usually means a simple configuration issue:
dbo
scheme (it might be in Fred.Categories
)dbo.CATEGORIES
Any of these will cause the above exception. In particular, you state:
My database contains 6 tables and one of them is Category.
Now to a machine, Category
!= Categories
Try using model builder class.It is the way to configure or explicitly define the mapping between table and model class.
In your entity/context class try adding this code
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Category>().ToTable("Category");
}
Its a method.Make sure ur using all the including statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With