Msdn how do i video series database




















DbContext worked out what classes to include in the model by looking at the DbSet properties that we defined. It then uses the default set of Code First conventions to determine table and column names, determine data types, find primary keys, etc.

To do this we are going to use a feature called Code First Migrations, or Migrations for short. Migrations allows us to have an ordered set of steps that describe how to upgrade and downgrade our database schema. Each of these steps, known as a migration, contains some code that describes the changes to be applied. In the previous section we looked at using Data Annotations to supplement or override what was detected by convention.

Most model configuration can be done using simple data annotations. The fluent API is a more advanced way of specifying model configuration that covers everything that data annotations can do in addition to some more advanced configuration not possible with data annotations.

Data annotations and the fluent API can be used together. In this walkthrough we looked at Code First development using a new database. We defined a model using classes then used that model to create a database and store and retrieve data.

Once the database was created we used Code First Migrations to change the schema as our model evolved. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy. After you finish creating the table, add some movie records to the table.

Enter a list of your favorite movies see Figure 5. Figure 05 : Entering movie records Click to view full-size image. We next need to create a set of classes to represent our database. We need to create a database model. We'll take advantage of the Microsoft Entity Framework to generate the classes for our database model automatically. The ASP. Follow these steps to complete the wizard:.

The Designer should display the Movies database table see Figure 7. We need to make one change before we continue. The Entity Data Wizard generates a model class named Movies that represents the Movies database table. Because we'll use the Movies class to represent a particular movie, we need to modify the name of the class to be Movie instead of Movies singular rather than plural.

Double-click the name of the class on the designer surface and change the name of the class from Movies to Movie. After making this change, click the Save button the icon of the floppy disk to generate the Movie class. The next step is to create the ASP. A controller is responsible for controlling how a user interacts with an ASP. After you complete these steps, the controller in Listing 1 is created.

Notice that it contains methods named Index, Details, Create, and Edit. In the following sections, we'll add the necessary code to get these methods to work. Figure 08 : Adding a new ASP. When you run an ASP.

We'll use the Index method to display the list of records from the Movies database table. We'll take advantage of the database model classes that we created earlier to retrieve the movie database records with the Index method.

The MoviesDBEntities class represents our database model and we'll use this class to communicate with our database. I've also modified the Index method in Listing 2. ToList returns a list of all of the movie records from the Movies database table. The list of movies is passed to the view. Anything that gets passed to the View method gets passed to the view as view data.

The Index method returns a view named Index. We need to create this view to display the list of movie database records. Follow these steps:. You should build your project select the menu option Build, Build Solution before opening the Add View dialog or no classes will appear in the View data class dropdown list. After you complete these steps, a new view named Index. The contents of the Index view are included in Listing 3.

Figure 09 : Adding a view from a controller action Click to view full-size image. Figure 10 : Creating a new view with the Add View dialog Click to view full-size image. The view contains a foreach loop that iterates through each movie represented by the ViewData. Model property. If you run your application by hitting the F5 key, then you'll see the web page in Figure Figure 11 : The Index view Click to view full-size image. The Index view that we created in the previous section includes a link for creating new database records.

Let's go ahead and implement the logic and create the view necessary for creating new movie database records. The Home controller contains two methods named Create.

The first Create method has no parameters. This overload of the Create method is used to display the HTML form for creating a new movie database record. The second Create method has a FormCollection parameter. This overload of the Create method is called when the HTML form for creating a new movie is posted to the server. This second Create method has been modified in the updated HomeController class in Listing 4.

The new version of the Create method accepts a Movie parameter and contains the logic for inserting a new movie into the Movies database table. I plan on continuing the series with more intermediate topics like custom object binding, adding validation and business logic, and how to manage requirements changes. What kind of topics are you interested in seeing here?

Post a comment or drop me a line and let me know. Those interested in the sample code I created in this series so far, get it here. Log in to join the discussion. May 8th, Marketing Director,.



0コメント

  • 1000 / 1000