.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Entity Framework : Basic CRUD operations with step by step

Posted By:Sathakkathul Haq       Posted Date: October 08, 2010    Points: 75    Category: ASP.NET    URL: http://www.dotnetspark.com  

This sample helps you to learn about the basic CRUD operations using Entity Framework.
 

Step 1:

Database:Automobiles

Fig 1:

 Fig 1 shows the table name: Vehicle contains fields of like Id,VehicleName and VehicleType

Step 2:

 Creates the .edmx from the .Net applications.

 

  Fig 2:

  Fig 2 shows about to select the ADO.NET Entity Data model with name.

  

  Fig 3:

  Fig 3 shows about to choose the model contents and click Next>

  

   Fig 4:

   Fig 4 shows about the Data Connection with New Connection

  

 

   Fig 5:

   Fig 5 shows about to choose about servername and database.

  

  Fig 6:

  Fig 6 shows about to choose the required table and click the Finish button.

 

  Fig 7:

  Fig 7 shows about the created Model.edmx in the solution explorer.

  If we click the Model.edmx, it shows entire related properties with mapping fields like below.

  

  Fig 7:

 Step 3:

 Now, we are trying to Create the records in the Vehicle table by using following code snippet by using AutomobileEntities which is mentioned in Fig4

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorkAround
{
    public class Program
    {
        
        static void Main(string[] args)
        {
            AutomobilesEntities context = new AutomobilesEntities();
            Vehicle veh = new Vehicle() { VehicleName = "Hyundai", VehicleType = "Car" };

            context.AddToVehicle(veh);
            context.SaveChanges();
            
        }
    }
}

 Step 4:

 Now,we are trying to Read the records from the vehicle table by using the following code snippet.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorkAround
{
    public class Program
    {
        
        static void Main(string[] args)
        {
            AutomobilesEntities context = new AutomobilesEntities();
           
            IEnumerable list = from v in context.Vehicle
                                        select v;

            foreach (var v in list)
            {
                Console.WriteLine(v.VehicleName);
            }

            Console.ReadKey(true);
            
        }
    }
}

 Step 5:

 Now, we are trying to Update the records in Vehicle table by using the following code snippet.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorkAround
{
    public class Program
    {
        
        static void Main(string[] args)
        {
            AutomobilesEntities context = new AutomobilesEntities();
           
            IEnumerable list = from v in context.Vehicle
                                        where v.VehicleType.Contains("Maruti")
                                        select v;

            foreach (var v in list)
            {
                v.VehicleType = 800 + "New";
               
            }
            context.SaveChanges();
        }
    }
}

 Step 6:

 Now, we are trying to Delete the records in Vehicle table by using the following code snippet.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorkAround
{
    public class Program
    {
        
        static void Main(string[] args)
        {
            AutomobilesEntities context = new AutomobilesEntities();

            Vehicle veh = context.Vehicle.First(v => v.Id == 6);

            context.DeleteObject(veh);
            context.SaveChanges();
        }
    }
}
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend