Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

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.

Life is a comedy to those who think; a tragedy to those who feel.
  • Administrators
Posted

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

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

public class Report
{
public int ReportId { get; set; }

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

//rest of class

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

See I tried that as well as seen below

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...

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.

Life is a comedy to those who think; a tragedy to those who feel.
Posted

Negative, tried that already as well.

 

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

 

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.

Life is a comedy to those who think; a tragedy to those who feel.
Posted

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..."

Life is a comedy to those who think; a tragedy to those who feel.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...