Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Monday, 27 August 2012

Video from MIX11 http://channel9.msdn.com/Events/MIX/MIX11/FRM13

GUI

Adding Athlete object

image

Using GUI to do the scaffolding… used this Template.

image
Got this error doing a many to many relationship on Scaffolding the MedalAwards controller.

public class Athlete
    {
        public int AthleteId { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
    }
    //many athletes can have many medaltypes
    public class MedalAward
    {
        public int Id { get; set; }
        public string Event { get; set; }

        //the FK property
        public int MedalTypeId { get; set; }
        public virtual MedalType MedalType { get; set; }

        //now link to athlete..could put AthleteId property here
        //[ForeignKey("WinnerId")]
        //public virtual Athlete Winner { get; set; }
        public int AthleteId { get; set; }
        public virtual Athlete Athlete { get; set; }

    }

    public class MedalType
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }


Shifted my model to a simple many to many.

image
Many to many working!

image
Alt V E O to get Package Manager Console
Making a repository pattern.

image
Templates knew that we’ll be need the 3 repos.  Does eager loading so not loads of db calls for each query.

image
get-package

public class Athlete
    {
        //public int AthleteId { get; set; }
        [Key] public Guid CompetitorUniqueRef { get; set; }
        [Required, StringLength(53)] public string Name { get; set; }
        public string Country { get; set; }
    }
    //many athletes can have many medaltypes
    public class MedalAward
    {
        public int Id { get; set; }
        public string Event { get; set; }

        //the FK property
        public int MedalTypeId { get; set; }
        public virtual MedalType MedalType { get; set; }

        //now link to athlete..could put AthleteId property here
        //[ForeignKey("WinnerId")]
        //public virtual Athlete Winner { get; set; }
        //public int AthleteId { get; set; }
        public Guid AthleteId { get; set; }
        public virtual Athlete Athlete { get; set; }
    }

changed the PK of athlete to be something different and not conventional.

image
after a re-scaffold

Seed Data (pre code Migrations)

//could use codeMigrations
    class MyInitialiser : DropCreateDatabaseIfModelChanges<OlympicsContext>
    {
        protected override void Seed(OlympicsContext context)
        {
            //sample medal types
            var gold = new MedalType {Name = "Gold"};
            var silver = new MedalType {Name = "Silver"};
            var bronze = new MedalType {Name = "Bronze"};

            foreach (var medalType in new[] {gold,silver,bronze})
                context.MedalTypes.Add(medalType);

            //sample athletes
            var dave = new Athlete { Name = "Dave Mateer", Country = "Great Britain", CompetitorUniqueRef = new Guid("96ac9b12-b992-4377-bd85-3dde1a760aa5") };
            var usain = new Athlete { Name = "Usain Bolt", Country = "Jamaica", CompetitorUniqueRef = new Guid("a283b455-b109-434c-8800-4a80f914cc38") };
            var bob = new Athlete { Name = "Bob Smith", Country = "New Zealand", CompetitorUniqueRef = new Guid("a9e55b45-e7f9-44bb-bd9e-d3c2a08b02ce") };

            foreach (var athlete in new[] { dave, usain, bob })
                context.Athletes.Add(athlete);

            //sample medal awards
            var awards = new[]
                             {
                                 new MedalAward {Event = "100m Sprint", Athlete = usain, MedalType = gold},
                                 new MedalAward {Event = "50k", Athlete = dave, MedalType = silver}
                             };
            foreach (var award in awards)
            {
                context.MedalAwards.Add(award);
            }

        }
    }

Putting in a seeder of data when the database has to be regenerated.

Fluent Mappings

image
Mapping what is in MVC to the DB.  This created a new table in the DB called PEOPLE, and a column called TERRITORY_OF_ORIGIN

Final Product

Keeping things conventional makes scaffolding easier to mock something up quickly, and to remember the syntax.

image
Many to many relationship

image

| | # 
# Monday, 14 March 2011

Downloaded EF CTP5 http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-released.aspx

Created a database and a model in EF

image

Added DbContext Generator (right click inside edmx file)

image

scaffold controller people –Force

Comment out generated dbcontext

Change peoplecontroller to point to new entities

image

Problem here is that FirstName is not required

image

yet in the model it is not nullable.

I can fix it by going to:

namespace EFTest.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public partial class Person
{
// Primitive properties

public int PersonID { get; set; }
public int Age { get; set; }
public System.DateTime DateOfBirth { get; set; }
[Required]
public string FirstName { get; set; }

}
and adding in a Required field using DataAnnotations.

| | #