Introduction
This article explains how to use the Code First Approach with the Fluent API in an ASP.NET MVC Application.
Step 1
Create a New MVC 4 Application and give it the name "Fluent API".

Now your new application will be ready for use. Now we need to add some Nuget to our application, so right-click on the name of your application in the Solution Explorer and choose to "Manage Nuget Package".

Step 2
In the Manage Nuget package Wizard search for "Entity Framework" and then click on the "Install" button to add this to your application. This will add assemblies to your project.

Now in the Solution Explorer right-click on the "Models" folder and choose to "Add a Class" and name it "FirstClass.cs". In this class you need to add the following code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Fluent_API.Models
- {
- public class Purchaser
- {
- public int PurchaserId { get; set; }
- public string PurchaserName { get; set; }
- public string MobileNo { get; set; }
- public string City { get; set; }
- public string District { get; set; }
- public string State { get; set; }
- public virtual ICollection<Requirement> Required { get; set; }
- }
- public class Requirement
- {
- public int Id { get; set; }
- public string Item { get; set; }
- public int Quantity { get; set; }
- public int UnitPrice { get; set; }
- public int TotalPrice { get; set; }
- public int? PurchaserId { get; set; }
- public virtual Purchaser Purchaser { get; set; }
- }
- }
Here we have provided two classes, one named "Purchaser" and the other named "Requirement". Both of these classes are inter-related to each other. In the Requirement class PurchaserId is the Foreign key.
Step 3
Now to allow the application to create the backup in the database, we need to provide the Connection String in the "Web.Config" file that will be like this:
- <connectionStrings>
- <add name="PurchaseConnectionString"
- connectionString="Data Source=MCNDESKTOP20;Initial Catalog=Purchased;User ID=sa;Password=*********"
- providerName="System.Data.SqlClient"/>
- </connectionStrings>
Here I first provided the name for the Connection String then I provided the name for the Database file that will be created in SQL.
Step 4
Now add a new class in the Models and name it "PurchaseClass.cs". In this class add the following code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations.Schema;
- using System.Data.Entity;
- namespace Fluent_API.Models
- {
- public class PurchaseClass : DbContext
- {
- public DbSet<Purchaser> Purchasers { get; set; }
- public DbSet<Requirement> Requirement { get; set; }
- public PurchaseClass():base("name=PurchaseConnectionString")
- {
- }
- protected override void OnModelCreating(DbModelBuilder x)
- {
- x.Entity<Purchaser>().HasKey(c => c.PurchaserId);
- x.Entity<Purchaser>().Property(c => c.PurchaserId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
- x.Entity<Purchaser>().Property(c =>c.PurchaserName).HasMaxLength(80);
- x.Entity<Purchaser>().Property(c => c.MobileNo).HasMaxLength(14);
- x.Entity<Purchaser>().Property(c => c.City).HasMaxLength(40);
- x.Entity<Purchaser>().Property(c => c.District).HasMaxLength(40);
- x.Entity<Purchaser>().Property(c => c.State).HasMaxLength(20);
- x.Entity<Requirement>().HasKey(o => o.Id);
- x.Entity<Requirement>().Property(o => o.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
- x.Entity<Requirement>().Property(o => o.Item).HasMaxLength(50);
- x.Entity<Requirement>().HasRequired(c => c.Purchaser)
- .WithMany(o => o.Required).HasForeignKey(o => o.PurchaserId);
- x.Entity<Requirement>()
- .HasRequired(c => c.Purchaser)
- .WithMany(o => o.Required)
- .HasForeignKey(o => o.PurchaserId)
- .WillCascadeOnDelete(true);
- base.OnModelCreating(x);
- }
- }
- }





Join the conversation! Your thoughts help the community grow.