Data Modeling MVC

PrOpHeT

Freshman
Joined
Sep 1, 2004
Messages
29
I am trying out my hand at data modeling, what I am trying to do is get an editor that allows me to create types with an FK reference in a main data table, and when the editors load, have selections rather than text fields.

http://www.asp.net/mvc/tutorials/mvc-music-store-part-5

I had assumed that the since MVC made the FK when it created the DB that is followed it as well when creating CRUD views, it appeared as though that was the case from the music store demo as their screen shows select lists and they even make a point to state that it uses them.

I have gone through the Music Store demo and I cannot tell what I have done wrong, model is as follows.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace MVCTests.Models
{

    public class MachineType {

        public int MachineTypeId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Type Display")]
        public string Name { get; set; }

    }

    public class ReportType
    {

        public int ReportTypeId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Type Display")]
        public string Name { get; set; }

    }

    public class Report
    {

        public int ReportId { get; set; }

        //[Required]
        [Display(Name = "Machine Type")]
        public int MachineTypeId { get; set; }

       // [Required]
        [Display(Name = "Report Type")]
        public int ReportTypeId { get; set; }

        [Required(ErrorMessage="Error.")]
        [DataType(DataType.Text)]
        [Display(Name = "Display")]
        public string display { get; set; }

        [Required(ErrorMessage = "Error.")]
        [DataType(DataType.Text)]
        [Display(Name = "Data Label")]
        public string data_label { get; set; }

        [Required(ErrorMessage = "Error.")]
        [DataType(DataType.Text)]
        [Display(Name = "Report File")]
        public string rpt_file { get; set; }

        [Required(ErrorMessage = "Error.")]
        [DataType(DataType.Text)]
        [Display(Name = "Query File")]
        public string qry_file { get; set; }

    }

    public class reportConfigurationEntitiy : DbContext
    {
        public DbSet<Report> Reports { get; set; }
        public DbSet<MachineType> MachineTypes { get; set; }
        public DbSet<ReportType> ReportTypes { get; set; }
    }


}

Many thanks in advance.
 
If you wish EF to allow you to navigate the relationship between the various entities then you need to create a property of the same type rather than just use the ID,

e.g. if you have a type like
C#:
public class MachineType 
{
public int MachineTypeId { get; set; }

[Required]
[DataType(DataType.Text)]
[Display(Name = "Type Display")]
public string Name { get; set; }
}

Then the report class would look more like
C#:
public class Report
{
public int ReportId { get; set; }

//[Required]
[Display(Name = "Machine Type")]
public MachineType MachineType { get; set; }

//rest of class
 
See I tried that as well as seen below
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace MVCTests.Models
{

    public class MachineType {

        public int MachineTypeId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Type Display")]
        public string Name { get; set; }

    }

    public class ReportType
    {

        public int ReportTypeId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Type Display")]
        public string Name { get; set; }

    }

    public class Report
    {

        public int ReportId { get; set; }

        //[Required]
        [Display(Name = "Machine Type")]
        public MachineType MachineTypeId { get; set; }

       // [Required]
        [Display(Name = "Report Type")]
        public ReportType ReportTypeId { get; set; }

        [Required(ErrorMessage="You must supply a display value for this report.")]
        [DataType(DataType.Text)]
        [Display(Name = "Display")]
        public string display { get; set; }

        [Required(ErrorMessage = "You must supply the label for this report's dataset(s).")]
        [DataType(DataType.Text)]
        [Display(Name = "Data Label")]
        public string data_label { get; set; }

        [Required(ErrorMessage = "This report must reference a valid RDLC file in the server's RPT directory path.")]
        [DataType(DataType.Text)]
        [Display(Name = "Report File")]
        public string rpt_file { get; set; }

        [Required(ErrorMessage = "This report must reference a valid SQL file in the server's QRY directory path.")]
        [DataType(DataType.Text)]
        [Display(Name = "Query File")]
        public string qry_file { get; set; }

    }

    public class reportConfigurationEntitiy : DbContext
    {
        public DbSet<Report> Reports { get; set; }
        public DbSet<MachineType> MachineTypes { get; set; }
        public DbSet<ReportType> ReportTypes { get; set; }
    }


}

When I do that the scaffolding engine simply does not create editors for those fields, when I tried to manually create one using Html.DropDownList() I get an error that the field is not an IEnumerable.

So I tried...
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace MVCTests.Models
{

    public class MachineType {

        public int MachineTypeId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Type Display")]
        public string Name { get; set; }

    }

    public class ReportType
    {

        public int ReportTypeId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Type Display")]
        public string Name { get; set; }

    }

    public class Report
    {

        public int ReportId { get; set; }

        //[Required]
        [Display(Name = "Machine Type")]
        public List<MachineType> MachineTypeId { get; set; }

       // [Required]
        [Display(Name = "Report Type")]
        public List<ReportType> ReportTypeId { get; set; }

        [Required(ErrorMessage="You must supply a display value for this report.")]
        [DataType(DataType.Text)]
        [Display(Name = "Display")]
        public string display { get; set; }

        [Required(ErrorMessage = "You must supply the label for this report's dataset(s).")]
        [DataType(DataType.Text)]
        [Display(Name = "Data Label")]
        public string data_label { get; set; }

        [Required(ErrorMessage = "This report must reference a valid RDLC file in the server's RPT directory path.")]
        [DataType(DataType.Text)]
        [Display(Name = "Report File")]
        public string rpt_file { get; set; }

        [Required(ErrorMessage = "This report must reference a valid SQL file in the server's QRY directory path.")]
        [DataType(DataType.Text)]
        [Display(Name = "Query File")]
        public string qry_file { get; set; }

    }

    public class reportConfigurationEntitiy : DbContext
    {
        public DbSet<Report> Reports { get; set; }
        public DbSet<MachineType> MachineTypes { get; set; }
        public DbSet<ReportType> ReportTypes { get; set; }
    }


}

Still no joy, it does not create editors, and manually creating Html.DropDownList() creates a null reference exception.

Tried to attach a project, but even the bare bones default project zips to > 2mb, so cannot attach.

Really there is nothing in this project other than this model yet anyway, it is just a test.

I feel certain I am overlooking something minor, generally the case, but I seem to be consistently overlooking it.
 
Negative, tried that already as well.

That results in no editor created in the default views as well.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace MVCTests.Models
{

    public class MachineType
    {

        public int MachineTypeId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Type Display")]
        public string Name { get; set; }

    }

    public class ReportType
    {

        public int ReportTypeId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Type Display")]
        public string Name { get; set; }

    }

    public class Report
    {

        public int ReportId { get; set; }

        [Required]
        [Display(Name = "Machine Type")]
        public virtual MachineType MachineTypeId { get; set; }

        [Required]
        [Display(Name = "Report Type")]
        public virtual ReportType ReportTypeId { get; set; }

        [Required(ErrorMessage = "You must supply a display value for this report.")]
        [DataType(DataType.Text)]
        [Display(Name = "Display")]
        public string display { get; set; }

        [Required(ErrorMessage = "You must supply the label for this report's dataset(s).")]
        [DataType(DataType.Text)]
        [Display(Name = "Data Label")]
        public string data_label { get; set; }

        [Required(ErrorMessage = "This report must reference a valid RDLC file in the server's RPT directory path.")]
        [DataType(DataType.Text)]
        [Display(Name = "Report File")]
        public string rpt_file { get; set; }

        [Required(ErrorMessage = "This report must reference a valid SQL file in the server's QRY directory path.")]
        [DataType(DataType.Text)]
        [Display(Name = "Query File")]
        public string qry_file { get; set; }

    }

    public class reportConfigurationEntitiy : DbContext
    {
        public DbSet<Report> Reports { get; set; }
        public DbSet<MachineType> MachineTypes { get; set; }
        public DbSet<ReportType> ReportTypes { get; set; }
    }


}

My understanding of virtual is that it makes it lazy load?

I am going to go back and very carefully read the tutorial again and see what I may have missed.
 
Well dawn breaks over marblehead...

I do believe I have found the issue to be the fact that it did not do so in the Music Store Demo either.

A reread and a quick look through the sample project, and I saw that they were creating select lists, feeding them to the viewbag, and using *those* to generate the Html.DropDownList().

"El diablo está en los detalles..."
 
Back
Top