Installation
The short version is this: Install-Package MvcScaffolding. If you understood that, do it and skip ahead to “Scaffolding a CRUD interface”. If not, read on.
1. Install ASP.NET MVC 3, which includes the excellent NuGet Package Manager.
2. Create or open an ASP.NET MVC 3 web application. I’m calling mine ‘SoccerSite’.
3. Install the MvcScaffolding package. You can install it using the NuGet Package Manager Console, so it only takes a few seconds and you don’t have to download anything using your browser. To do so,
- Open the Package Manager Console window using Visual Studio’s View->Other Windows->Package Manager Console menu item.
- Enter the following:
Scaffolding a CRUD interface
Scaffold Controller Team –Repository -Force
Installing or Upgrading MvcScaffolding
Update-Package MvcScaffoldingDefining simple relations between model classes
I’m going to continue the tutorial in the introductory post, which assumes you’ve already installed created the following model class:Now I’d like to define another model class, Player. Each Player is associated with a team:Simply having the TeamId property on Player is enough for both MvcScaffolding and EF Code First to realise there’s a 1:many relationship. Actually MvcScaffolding supports two conventions for defining relations – this is the simple one; I’ll explain the alternative later.Note that defining TeamId as a non-nullable int, it’s mandatory. Each player must be in a team. If you wanted the relationship to be optional, use a nullable link property instead (i.e., public int? TeamId { get; set; }).If you scaffold your UI now, by executing the following commands…… and by telling Team to have a property that holds its Players:Notice that both of these new properties are marked virtual. This lets Entity Framework use its Lazy Loading feature so the associated entities will be fetched as needed, and spares you having to write code to fetch them in your controller. I’ll talk more about this in a moment.Now if you were to scaffold the UI with models like this, by issuing the following commands: