Entity conventions
Conventions in entity framework
Code First enables you to describe a model by using C# classes. The basic shape of the model is detected by using conventions. Conventions are sets of rules that are used to automatically configure a conceptual model based on class definitions when working with Code First.
The conventions are defined in the System.Data.Entity.ModelConfiguration.Conventions
namespace. Discussion about the conventions in detail are not in the scope of this module you can refer to the below link for more details:
For this example, let us see how we can get rid of the Id column issue by removing them from the views.
Removing Id from views
Remove the Id field related markup (shown below for convenience) from all the views that are using it.
<div class="form-group">
<label asp-for="Id" class="control-label"></label>
<input asp-for="Id" class="form-control" />
<span asp-validation-for="Id" class="text-danger"></span>
</div>
//---
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Id)
</dt>
//---
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
The modified view files are also attached below in case you want to refer to them:
Run the app again
Let us run the app again and verify the crud operations end-to-end. We will notice that all the functionalities are working on our demo app!
Repository link
Completed app is available on GitHub with the following commit ID:
b807b7bc702d11039475f450aaf1bc479e76eb5c
Last updated