ASPNET 4 MVC 4 How Models Work
STEPS
1) Start Visual Studio an open MvcMusicStore Solution.
2) Observe the AppData folder of the project
2-1) Find the AppData item in your Solution Explorer
2-2) Right-click the item and select “Open Folder In File Explorer”
3) Run and register an account
3-1) Press F5 Key.
3-2) Click Register Link.
3-3) Notice that the database file is automatically created the moment you click the Register link
3-4) Enter user details
3-5) Notice that you have been logged in.
3-6) Log out and stop project execution.
4) Observe the Database
4-1) Go to Menu Bar. Select View/Server Explorer
4-2) Right-click UserProfile Table and select Show Table Data.
4-3) The record that you have just entered appears in the table.
5) Observe the Model that builds the Database
5-1) Switch to Solution Explorer and select Models/UserProfile.
6) Creating New Data Models
6-1) Right-click Models item, select Add/Class..
6-2) Type a name “Album.cs”
6-3) Data Model Class is created.
6-4) Replace the public class Album declaration with the following codes:
6-5) Repeat Step 6-1 to 6-3 to create other Data Models with the following codes:
6-6) Go to Menu Bar. Select Build/Build Solution.
7) Adding Controller Class for the Model
7-1) Right-click the Controllers, select Add/Controller…
7-2) Enter Controller details.
7-3) Type “MusicStoreDB”
7-4) Outcome (SCAFFOLDING EFFECT)
7-5) Inspect the codes for MusicStoreDB.cs
8) Modify Global.asax
8-1) Copy Auto-Generate Codes
8-2) Paste to Global.asax file
8-3) Register additional namespaces to simplify the codes
8-4) Press F5 Key to execute your project
8-5) Notice that a new database has been created
8-6) Stop project execution
8-7) Insert a seed method to Global.asax
8-8) Edit line no.21 so that it calls the seed method
8-9) Press F5 Key to execute the project
8-10) Notice that there is an initial record in the table
8-11) Click the Edit link
8-12) Notice that there is a drop-down list that limits user entry based on predefined set of values
This tutorial is about MvcMusicStore Solution which continues from previous tutorial, http://visualstudio-steps.blogspot.com/2014/09/aspnet-4-mvc-4-how-views-work.html.
STEPS
1) Start Visual Studio an open MvcMusicStore Solution.
2) Observe the AppData folder of the project
2-1) Find the AppData item in your Solution Explorer
2-2) Right-click the item and select “Open Folder In File Explorer”
At the moment the folder is empty.
3) Run and register an account
3-1) Press F5 Key.
3-2) Click Register Link.
3-3) Notice that the database file is automatically created the moment you click the Register link
3-4) Enter user details
3-5) Notice that you have been logged in.
3-6) Log out and stop project execution.
4) Observe the Database
4-1) Go to Menu Bar. Select View/Server Explorer
4-2) Right-click UserProfile Table and select Show Table Data.
4-3) The record that you have just entered appears in the table.
5) Observe the Model that builds the Database
5-1) Switch to Solution Explorer and select Models/UserProfile.
Notice the codes that actually define the table is shown on screen.
6) Creating New Data Models
6-1) Right-click Models item, select Add/Class..
6-2) Type a name “Album.cs”
6-3) Data Model Class is created.
6-4) Replace the public class Album declaration with the following codes:
public class Album
{
public virtual int AlbumId { get; set; }
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
|
Info:
Virtual properties are not required, but they do give EF a hook into your plain C# classes and enable features like an efficient change tracking mechanism. The EF needs to know when a property value on a model changes, because it might need to issue a SQL UPDATE statement to reconcile those changes with the database.
|
6-5) Repeat Step 6-1 to 6-3 to create other Data Models with the following codes:
public class Artist
{
public virtual int ArtistId { get; set; }
public virtual string Name { get; set; }
}
|
public class Genre
{
public virtual int GenreId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual List<Album> Albums { get; set; }
}
|
6-6) Go to Menu Bar. Select Build/Build Solution.
7) Adding Controller Class for the Model
7-1) Right-click the Controllers, select Add/Controller…
7-2) Enter Controller details.
7-3) Type “MusicStoreDB”
7-4) Outcome (SCAFFOLDING EFFECT)
Info:
Scaffolding in ASP.NET MVC can generate the boilerplate code you need for create, read, update,
and delete (CRUD) functionality in an application. The scaffolding templates can examine the type
definition for a model (such as the Album class you’ve created), and then generate a controller and
the controller’s associated views. The scaffolding knows how to name controllers, how to name
views, what code needs to go in each component, and where to place all these pieces in the project
for the application to work.
|
7-5) Inspect the codes for MusicStoreDB.cs
Refer point 2 in the screenshot above. To auto-generate database, we have to modify Global.asax file to include additional codes.
8) Modify Global.asax
8-1) Copy Auto-Generate Codes
(Refer codes in Step 7-5. Copy line no. 14)
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<MvcMusicStore.Models.MusicStoreDB>());
|
8-2) Paste to Global.asax file
8-3) Register additional namespaces to simplify the codes
Refer Step 8-1. There are two namespaces that can be added:
System.Data.Entity
MvcMusicStore.Models
8-4) Press F5 Key to execute your project
Add StoreManager to the localhost URL.
8-5) Notice that a new database has been created
8-6) Stop project execution
8-7) Insert a seed method to Global.asax
public class MusicStoreDbInitializer: DropCreateDatabaseAlways<MusicStoreDB>
{
protected override void Seed(MusicStoreDB context)
{
context.Artists.Add(new Artist { Name = "Al Di Meola" });
context.Genres.Add(new Genre { Name = "Jazz" });
context.Albums.Add(new Album
{
Artist = new Artist { Name = "Rush" },
Genre = new Genre { Name = "Rock" },
Price = 9.99m,
Title = "Caravan"
});
base.Seed(context);
}
}
|
8-8) Edit line no.21 so that it calls the seed method
Database.SetInitializer(new MusicStoreDbInitializer());
|
8-9) Press F5 Key to execute the project
8-10) Notice that there is an initial record in the table
8-11) Click the Edit link
8-12) Notice that there is a drop-down list that limits user entry based on predefined set of values
DOWNLOAD
---
No comments:
Post a Comment