I am developing a multi-tiered, ASP.NET MVC web application, but have not been able to progress further because I am unable to retrieve accurate object representations of my data within my application. Why is this so? I have tried to be as detailed as possible, but please request clarification if you wish to know more. My projects are as follows:
-MyProject.Data
-MyProject.Logic
-MyProject.Objects
-MyProject.Web
My entity classes from MyProject.Objects.Entities
public class Report
{
[Key]
public int Id { get; set; }
public string ReportName { get; set; }
public int ProductId { get; set; }
}
public class Product
{
[Key]
public int Id { get; set; }
public string ProductName { get; set; }
public ICollection<Report> ProductReports { get; set; }
}
My Context from MyProject.Data
public class MyDb : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Report> Reports { get; set; }
}
My controller from MyProject.Web
public class HomeController : Controller
{
MyDb _db = new MyDb();
public ActionResult Index()
{
var model =
from p in _db.Products
orderby p.ProductName ascending
select p;
return View(model);
}
}
Lastly, my Seed()
method from MyProject.Data.Migrations.Configuration
protected override void Seed(MyProject.Data.MyDb context)
{
context.Products.AddOrUpdate(r => r.ProductName,
new Product
{
ProductName = "AAA",
ProductReports =
new List<Report> {
new Report { ReportName = "Report A" },
new Report { ReportName = "Report B" },
new Report { ReportName = "Report C" }
}
},
new Product
{
ProductName = "MSI",
ProductReports =
new List<Report> {
new Report { ReportName = "Report X" },
new Report { ReportName = "Report Z" }
}
});
}
I can not tell why I am unable to get ICollection<Report> ProductReports
from MyProject.Objects.Entities.Product to populate with the correct values. Here are my results from Index()
in MyProject.Web
I thought at first, being slightly new to this framework, that I had some issues relating to the data structure. To my knowledge, my data appears to be set up correctly. Here is my data in SQL server
To further demonstrate that the data appears to be correct, I have run the following query to assure myself that the relations are set properly. I will provide the results alongside the query.
SELECT * From Reports INNER JOIN Products ON Reports.ProductId = Products.Id