Wednesday, 22 May 2013

ADO.NET Entity Framework:: Insert Update and Delete


Every .NET developer sometime will start using ADO.NET Entity Framework.
And first question will be such as how to insert, update and delete records.
Performing basic Inser, Update and Delete operations via the Entity Framework is very straight forward.

first you need to create a Database Table -
Script-
Create Database StudentDetail

USE [StudentDetail]
GO

CREATE TABLE [dbo].[login](
[loginid] [varchar](20) NOT NULL,
[password] [varchar](20) NOT NULL,
         [rights] [varchar](20) Null
)

after that you need to create a new Project windows or Web as per your Knowledge and Need..

Right Click on Project in SolutionExplorer Add -> Add New Item .opens a Pop Window , Add new edmx File like this.



Rename the file Name Click Add. it opens a new Popup Window Data Model Wizard. select Generate From Database click on Next Button . Click on New Connection Button , Connection Properties Popup Opens. Change Datasource as Microsoft SQL Server. Provide Server Name, Database name and Click OK button.
Entity Class name is byDefault Entered in TextBox like Below , Click on Next.


in the Next Popup Select Table from Database Click on Finish Button. Your edmx file Looks like This


Now you have to Write the Code for CRUD operation...

for insert Operation use the below Code  in Your Function 
     using (StudentDetailEntities std = new StudentDetailEntities())
            {
           #region  Code for insert row in Table using EF
           login log = new login() { loginid = "S009", password = "pass", rights = "user" };
                //Add to memory
                std.AddTologins(log);
                //Save to database
                std.SaveChanges();
      #endregion
          }


for Update Operation use the below Code  in Your Function 


          using (StudentDetailEntities std = new StudentDetailEntities())
            {
       #region  Code for Update row in Table using EF
                //Get the specific LoginID from Database
            login _log = (from log in std.logins where log.loginid == "S009" select              log).First();
                //Change the LoginID in memory
                _log.password = "s009";
                //Save to database
                std.SaveChanges();
         #endregion

             }

for Delete Operation use the below Code  in Your Function 

           using (StudentDetailEntities std = new StudentDetailEntities())
            {
              #region  Code for Delete row in Table using EF
              login _login= (from log in std.logins where log.loginid== "s009" select log).First();
                std.DeleteObject(_login);
                std.SaveChanges();
                #endregion
           
          }
---------------------------------------------------------------------------------------------

this is the Simple Code ......
Happy coding!

Thanks and Regards
SUraj K Mad.




4 comments:

  1. Another generic way of updating using EF could be like given below :-

    [HttpPost]
    public ActionResult Edit(Login _login)
    {
    using (StudentDetailEntities std = new StudentDetailEntities())
    {

    std.Logins.AddObject(_login);
    std.ObjectStateManager.ChangeObjectState(_login,System.Data.EntityState.Modified);
    //Save to database
    std.SaveChanges();
    }

    }

    ReplyDelete
    Replies
    1. replace "_login,System.Data.EntityState.Modified" with _login,System.Data.EntityState.Added

      Delete

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (9) Web Service (1) XMl (1)