How to Perform Master Detail CRUD Operations Using Entity Framework and ASP.NET MVC 3 in C#
In this article, you will learn how to perform master detail CRUD (Create, Read, Update, Delete) operations using Entity Framework and ASP.NET MVC 3 in C#. Master detail CRUD operations are useful when you want to display and manipulate data from two related tables in a single view. For example, you may want to show a list of customers and their orders, or a list of products and their categories.
Master Detail CRUD Operations Using EF And ASPnet MVC 3 In C
Download File: https://www.google.com/url?q=https%3A%2F%2Furlin.us%2F2vTa8j&sa=D&sntz=1&usg=AOvVaw0Tm0uKl9em179I6HxNgLBq
To demonstrate this scenario, we will use a simple database that contains two tables: Customers and Orders. The Customers table has the following fields: CustomerID (primary key), Name, Email, and Phone. The Orders table has the following fields: OrderID (primary key), CustomerID (foreign key), ProductName, Quantity, and Price. Each customer can have multiple orders, but each order belongs to only one customer.
We will use Entity Framework Code First approach to create the database and the model classes from our code. We will also use ASP.NET MVC 3 to create the controller and the views that will handle the master detail CRUD operations. We will use C# as the programming language.
Step 1: Create the Model Classes
The first step is to create the model classes that represent our database tables. We will use the [System.ComponentModel.DataAnnotations](https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations?view=net-5.0) namespace to add some data annotations that will help Entity Framework to generate the database schema and validate the data.
Create a new class named Customer.cs and add the following code:
```csharp
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace MasterDetailCrud.Models
public class Customer
[Key]
public int CustomerID get; set;
[Required]
[StringLength(50)]
public string Name get; set;
[Required]
[EmailAddress]
public string Email get; set;
[Phone]
public string Phone get; set;
// Navigation property to link with Orders table
public virtual ICollection Orders get; set;
```
Create another class named Order.cs and add the following code:
```csharp
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MasterDetailCrud.Models
public class Order
[Key]
public int OrderID get; set;
// Foreign key to link with Customers table
[ForeignKey("Customer")]
public int CustomerID get; set;
[Required]
[StringLength(100)]
public string ProductName get; set;
[Required]
[Range(1, 100)]
public int Quantity get; set;
[Required]
[DataType(DataType.Currency)]
public decimal Price get; set;
// Navigation property to link with Customers table
public virtual Customer Customer get; set;
```
Step 2: Create the Database Context Class
The next step is to create the database context class that will manage the connection with the database and expose the model classes as DbSet properties. We will use the [System.Data.Entity](https://docs.microsoft.com/en-us/dotnet/api/system.data.entity?view=entity-framework-6.2.0) namespace to access Entity Framework functionalities.
Create a new class named MasterDetailContext.cs and add the following code:
```csharp
using System.Data.Entity;
namespace MasterDetailCrud.Models
public class MasterDetailContext : DbContext
// Constructor to specify the connection string name
public MasterDetailContext() : base("name=MasterDetailConnection")
// Enable migrations to update the database schema automatically
Database.SetInitializer(new MigrateDatabaseToLatestVersion());
// DbSet properties to access Customers and Orders tables
public DbSet Customers get; set;
public DbSet Orders get; set;
```
Step
e033bf56a8