Skip to content

Instantly share code, notes, and snippets.

@am11
Last active December 16, 2015 11:38
Show Gist options
  • Save am11/5428761 to your computer and use it in GitHub Desktop.
Save am11/5428761 to your computer and use it in GitHub Desktop.
MVC 4 Kick Start.. Code first
Create a new MVC 4 project and add four classes under Models: Round.cs, Tournament.cs, Team.cs and RoundTeam.cs.
Add this in Round.cs file:
public class Round
{
public int Id { get; set; }
public int TournamentId { get; set; }
// Navigation property to your link table
public virtual ICollection<RoundTeam> RoundTeams { get; set; }
public virtual Tournament Tournament { get; set; }
// If you wanted to have a property direct to the temas, just add this
public IEnumerable<Team> Teams
{
get
{
return RoundTeams.Select(ft => ft.Team);
}
}
// Similarly this is to Retrieve Winner Teams
public IEnumerable<Team> GetWinnerTeams
{
get
{
return RoundTeams.Where(ft => ft.IsWinner.GetValueOrDefault(false)).Select(ft => ft.Team);
}
}
}
In Team.cs, paste this code:
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public int Size { get; set; }
// Navigation property to your link table
public virtual ICollection<RoundTeam> RoundTeams { get; set; }
// If you wanted to have a property direct to the rounds, just add this:
public IEnumerable<Round> Rounds
{
get
{
return RoundTeams.Select(ft => ft.Round);
}
}
}
In Tournament.cs, paste this:
public class Tournament
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
[Required]
public virtual ICollection<Round> Rounds { get; set; }
public Tournament()
{
Rounds = new List<Round>();
}
}
and finally in RoundTeam, add this:
public class RoundTeam
{
[Key, Column(Order = 0)]
public int RoundId { get; set; }
[Key, Column(Order = 1)]
public int TeamId { get; set; }
public bool? IsWinner { get; set; }
// Navigation properties to the outer tables
[Required]
public virtual Round Round { get; set; }
[Required]
public virtual Team Team { get; set; }
}
In PMC, run this grouped command:
Install-Package MvcScaffolding; if($?) {Scaffold Controller Team}; if ($?) {Scaffold Controller Round}; if($?) {Scaffold Controller Tournament}
Now identify your context name: Models > Whatever1Context.cs... where Whatever1Context is your context name.. and replace it in the following line and run it in PMC
Enable-Migrations -ContextTypeName MvcApplication1.Models.Whatever1Context
It will create the file Configuration.cs under Migrations..
Also, since we didn't scaffolded RoundTeam join table, add this line at the end of Context class in your WhateverContext file:
public DbSet<TournamentManager.Models.RoundTeam> RoundTeams { get; set; }
In Configuration.cs, you will find Seed method. Seeds are use to insert initial records in DB... it could be dummy records for testing or real data for prepopulation of db! If you are looking for a long list of records, you can use script (JavaScript or PowerShell/posh) to generate these tuples:
protected override void Seed(TournamentManager.Models.MvcApplication1Context context)
{
// Instantiate Tournaments
var TournamentsList = new List<Tournament>
{
new Tournament { Name = "Champs tournament", StartDate = DateTime.Now, EndDate = DateTime.Parse("2013-9-2") },
new Tournament { Name = "School tournament", StartDate = DateTime.Now, EndDate = DateTime.Parse("2013-8-3") }
};
TournamentsList.ForEach(a => context.Tournaments.AddOrUpdate(a));
// Instantiate Teams
var TeamsList = new List<Team>
{
new Team { Size = 10, Name = "List1: First Team" },
new Team { Size = 10, Name = "List1: Second Team" },
new Team { Size = 10, Name = "List1: Third Team" },
new Team { Size = 10, Name = "List1: Fourth Team" },
new Team { Size = 10, Name = "List1: Fifth Team" },
new Team { Size = 10, Name = "List2: First Team" },
new Team { Size = 10, Name = "List2: Second Team" },
new Team { Size = 10, Name = "List2: Third Team" },
};
TeamsList.ForEach(a => context.Teams.AddOrUpdate(a));
// Instantiate Rounds
var RoundsList = new List<Round>
{
new Round { Tournament= TournamentsList[0] },
new Round { Tournament= TournamentsList[1] },
new Round { Tournament= TournamentsList[0] },
new Round { Tournament= TournamentsList[1] },
new Round { Tournament= TournamentsList[0] },
new Round { Tournament= TournamentsList[1] },
};
RoundsList.ForEach(a => context.Rounds.AddOrUpdate(a));
// Associate Rounds with Teams
var RoundTeamsList = new List<RoundTeam>
{
new RoundTeam { Team = TeamsList[0], Round = RoundsList[1], IsWinner = true },
new RoundTeam { Team = TeamsList[1], Round = RoundsList[0] },
new RoundTeam { Team = TeamsList[0], Round = RoundsList[2], IsWinner = true },
new RoundTeam { Team = TeamsList[1], Round = RoundsList[3] },
new RoundTeam { Team = TeamsList[2], Round = RoundsList[0] },
new RoundTeam { Team = TeamsList[3], Round = RoundsList[1], IsWinner = false }
};
RoundTeamsList.ForEach(a => context.RoundTeams.AddOrUpdate(a));
}
Build the project Ctrl+Shift+B, and run the following command in PMC to initialize the DB
add-migration Initial
This will create a file somenumber_Initial.cs under Migrations. Follow the instructions in this file to create table in DB. Then seed the DB with the content:
update-database
Open AppData > RouteConfig.cs and change default controller to Tournament and keep the action Index. You can always place a link to tournament controler in View>Home>Index.cshtml for dashboarding and navigation.
Run the project.
Now, under Views > Tournament or Records or Teams, you will find the Index, Create, Edit etc files. Also there you would find a partial file (_CreateOrEdit.cshtml) for Edit and Create as the form for both views is pretty much the same. Partials are used to extract out commonalitites. Partial files name start with underscore.
In create or edit partial, observe the form in Razor syntax and how the DropDown controls are created with MVC magic without bothering for SQL.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment