Question

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

enter image description here



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

enter image description here




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


enter image description here

Was it helpful?

Solution

As mentioned it looks like you are missing the mapping:

public class Product
{
  [Key]
  public int ProductID { get; set; }
  public string ProductName { get; set; }
  public virtual ICollection<Report> ProductReports { get; set; }
}

public class Report
{
  [Key]
  public int ReportID { get; set; }
  public string ReportName { get; set; }
  public int ProductID { get; set; }
  public virtual Product Product { get; set; }
}

The name of the mapping fields must be the same in both tables and the mapping will be done for you automatically using EF. Also you can add a virtual property to the report so that within the report you can call Product.ProductName or whatever.

Hope this helps

OTHER TIPS

It would be something like this:

public class ReportMap : EntityTypeConfiguration<Report>
    {
        public ReportMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Properties
            this.Property(t => t.ReportName)
                .IsRequired()
                .HasMaxLength(100);

            // Table & Column Mappings
            this.ToTable("Reports");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.ReportName).HasColumnName("ReportName");

            // Relationships
            this.HasRequired(t => t.Product)
                .WithMany(t => t.ProductReports)
                .HasForeignKey(d => d.ProductId);
        }
    }

And change your Report to also have a Product property, like such:

public class Report
{
    [Key]
    public int Id { get; set; }
    public string ReportName { get; set; }
    public int ProductId { get; set; }
    public virtual Product Product { get; set; }
}

Then in your context:

public partial class SomeContext : DbContext
{
    static SomeContext()
    {
        Database.SetInitializer<SomeContext>(null);
    }

    public SomeContext()
        : base("Name=SomeContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ReportMap());
    }
}

Here is an example of EF relationship I have working:

public class CourseSection
{
    [Key]
    public int CourseSectionID { get; set; }

    public int CourseID { get; set; }

    public string Title { get; set; }

    public virtual ICollection<SectionContent> SectionContents { get; set; }
}

public class Course
{
    [Key]
    public int CourseID { get; set; }

    [StringLength(50)]
    public string Name { get; set; }

    public virtual ICollection<CourseSection> CourseSections { get; set; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top