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](20NOT NULL,
 [password] [varchar](20NOT NULL,
  [rights] [varchar](20Null
)

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
           
          }

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

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives