ASP Module 2
Handling Data in MVC
Handling Data in MVC
  • Entity Framework Core
    • Overview
    • Setup
  • Persisting Data
    • Injecting the context
    • Saving changes
    • Entity conventions
  • Validating Data
    • Adding validation
    • Working of validation
    • Formatting data
  • Misc
    • Scaffolding Everything
Powered by GitBook
On this page
  • Conventions in entity framework
  • Removing Id from views
  • Run the app again
  • Repository link
  1. Persisting Data

Entity conventions

PreviousSaving changesNextAdding validation

Last updated 2 years ago

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
GitHub - raviramorg/MvcMovie: For learning ASP.NET CoreGitHub
Logo
Code First Conventions - EF6docsmsft
Logo
2KB
Create.cshtml
1KB
Delete.cshtml
1KB
Details.cshtml
2KB
Edit.cshtml
1KB
Index.cshtml