Menu Case Study Part 1 – Using MVC to Create Entry Screens
Prerequisites
You will need some basic knowledge in MVC and should at a minimum be familar with the steps of an MVC project. There are some video lectures here Lecture – Model Based Development in MVC Part 1 to get you started. This is part of the Case Study – Menu Example
Next: Lecture – Menu Case Study Part 2 – Creating a Custom View
Summary
After using the Wizard to create a MVC project in Visual Studio 2015 we create a complex data model using Code First and work through the steps associated with handling the key relationships and building an interface for the project. This shows common errors that occur, it uses Fluent API and handles multiple foreign key relations in Code First methodology.
Video
Reference Materials
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
namespace COP4709MenuDatabase.Models
{
public class COP4709MenuDatabaseContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public COP4709MenuDatabaseContext() : base("name=COP4709MenuDatabaseContext")
{
}
public System.Data.Entity.DbSet<COP4709MenuDatabase.Models.Menu> Menus { get; set; }
public System.Data.Entity.DbSet<COP4709MenuDatabase.Models.MenuGroup> MenuGroups { get; set; }
public System.Data.Entity.DbSet<COP4709MenuDatabase.Models.MenuItem> MenuItems { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Shown are examples of bypassing the cascade delete, either for each table OR for the entire DbContext
//
// modelBuilder.Entity<MenuGroup>().HasOptional(m => m.Menu).WithOptionalDependent().WillCascadeOnDelete(false);
// modelBuilder.Entity<MenuItem>().HasOptional(m => m.Menu).WithOptionalDependent().WillCascadeOnDelete(false);
// modelBuilder.Entity<MenuItem>().HasOptional(m => m.MenuGroup).WithOptionalDependent().WillCascadeOnDelete(false);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
base.OnModelCreating(modelBuilder);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace COP4709MenuDatabase.Models
{
public class Menu
{
[Key]
public int MenuID { get; set; }
[Display(Name ="Title")]
public string MenuTitle { get; set; }
[Display(Name ="Description")]
public string MenuDescription { get; set; }
public virtual List<MenuItem> MenuItems { get; set; }
public virtual List<MenuGroup> MenuGroups { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace COP4709MenuDatabase.Models
{
public class MenuGroup
{
[Key]
public int MenuGroupID { get; set; }
[ForeignKey("Menu")]
public int MenuID { get; set; }
public virtual Menu Menu { get; set; }
[Required(ErrorMessage="A title is required for the menu group")]
[Display(Name = "Title")]
public string MenuGroupTitle { get; set; }
[Display(Name = "Description")]
public string MenuGroupDescription { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace COP4709MenuDatabase.Models
{
public class MenuItem
{
[Key]
public int MenuItemID { get; set; }
[ForeignKey("Menu")]
public int MenuID { get; set; }
public virtual Menu Menu { get; set; }
[ForeignKey("MenuGroup")]
public int MenuGroupID { get; set; }
public virtual MenuGroup MenuGroup { get; set; }
[Required(ErrorMessage = "A title is required for the menu item")]
[Display(Name = "Title")]
public string MenuItemTitle { get; set; }
[Display(Name = "Description")]
public string MenuItemDescription { get; set; }
[Display(Name = "Nutrition Information")]
public string MenuitemNutrition { get; set; }
[Display(Name = "Information about Ingredients")]
public string MenuItemIngredients { get; set; }
[Display(Name = "How many in serving")]
public int MenuItemQuantity { get; set; }
[Display(Name = "Cost of Menu Item")]
public Single MenuItemCost { get; set; }
}
}
Next
Lecture – Menu Case Study Part 2 – Creating a Custom View