This will provide you a screen to create new entries in the recipes table.A screenshot is shown below − Once you press the Create button to create a new recipe, your record is added into the recipes table and it shows the following result − You can see the option to edit, show, and destroy the records. You can also list down all the recipes available in the recipes table using the URL To understand this, add some validation rules to the empty recipe model − Modify app/models/as follows and then test your application − It will generate the result as shown above output images.
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.
Below the Program class definition in add the following two classes. This enables the Lazy Loading feature of Entity Framework. Db Context and exposes a typed Db Set That is all the code we need to start storing and retrieving data.
You’ll notice that we’re making the two navigation properties (Blog. Lazy Loading means that the contents of these properties will be automatically loaded from the database when you try to access them. Obviously there is quite a bit going on behind the scenes and we’ll take a look at that in a moment but first let’s see it in action. This code creates a new instance of our context and then uses it to insert a new Blog.
Data annotations and the fluent API can be used together.
To access the fluent API you override the On Model Creating method in Db Context.
Later in this walkthrough we’ll look at how you can override these conventions.
Now it’s time to make some changes to our model, when we make these changes we also need to update the database schema.
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.
So far we’ve just let EF discover the model using its default conventions, but there are going to be times when our classes don’t follow the conventions and we need to be able to perform further configuration.