Thursday 30 April 2015

entity framework insert update delete

As in previous article explained that how we can create the entity frame work classes and how we can use it. 
Refer below link for the same;


Now in This Session will explain you how to perform the insert update delete operation in entity framework.


Suppose you have the table like below



You have already run the wizard and you got all the classes and helping classes of entity frame work,


To display the list of records into the grid, you need to use the command in Linq query to get the records of entity framework select items.


EntityFramework Select Operation

Get the records into data base through entity frame work.

It will be like below; As in below code I have joined the another class also to get the consolidate response of other class.
public List<BookDetails> GetBookDetail()
        {
            using (LibraryManagementSystemEntities obj = new LibraryManagementSystemEntities())
            {
                return (from oBook in obj.BookDetails
                        join oCat in obj.BookCategories on oBook.BookCategoryId equals oCat.BookCategoryId
                        select new BookDetails
                        {
                            BookDetailId = oBook.BookDetailId,
                            BookName = oBook.BookName,
                            BookAuthor = oBook.BookAuthor,
                            Price = oBook.Price,
                            NoofBookAvilable = oBook.NoofBookAvilable,
                            BookCategory =   new BookCategorys() {BookCategoryId =  oCat.BookCategoryId ,BookCategory  = oCat.BookCategoryName },
                            NoOfbooks = oBook.NoOfbooks,
                            CreatedDate = oBook.CreatedDate
                        }).ToList();
            }
        }


EntityFramework Insert Operation

Inserting the records into data base through entity frame work.
Above two lines of code help to insert the records into database. We will frame the object and call the method registerBookDetails and pass the Object of bookdetail as an argument.

  public int RegisterBookDetail(BookDetails oName)
        {
            using (LibraryManagementSystemEntities obj = new LibraryManagementSystemEntities())
            {
                BookDetail oData = new BookDetail();
                oData.BookName = oName.BookName;
                oData.BookAuthor = oName.BookAuthor;
                oData.Price = oName.Price;
                oData.NoOfbooks = oName.NoOfbooks;
                oData.NoofBookAvilable = oName.NoOfbooks;
                oData.BookCategoryId = oName.BookCategory.BookCategoryId ;
                oData.CreatedBy = oName.CreatedBy;
                oData.CreatedDate = DateTime.Now;
                oData.ModifiedBy = DateTime.Now;

                obj.BookDetails.Add(oData);

                return obj.SaveChanges();
            }
        }

EntityFramework Update Operation

Updating the records into data base through entity frame work.
Before call the savechanges method we need to just query the collection of entity frame work and on the result only need to find the collections and call the savechanges method.
public int UpdateBookDetail(BookDetails oName)
        {
            using (LibraryManagementSystemEntities obj = new LibraryManagementSystemEntities())
            {
                var oData = obj.BookDetails.First(i => i.BookDetailId == oName.BookDetailId);

                oData.BookName = oName.BookName;
                oData.BookAuthor = oName.BookAuthor;
                oData.Price = oName.Price;
                oData.NoOfbooks = oName.NoOfbooks;
                oData.NoofBookAvilable = oName.NoOfbooks;
                oData.BookCategoryId = oName.BookCategory.BookCategoryId;
                oData.ModifiedBy = DateTime.Now;
                return obj.SaveChanges();
            }
        }


EntityFramework Delete Operation

Delete the records into data base through entity frame work.
Just need to query the object that you want to delet and pass that object to remove method and after that you just need to call the Save Changes method.
public int DeleteBookDetail(int BookId)
        {
            using (LibraryManagementSystemEntities obj = new LibraryManagementSystemEntities())
            {
                var oSelect = obj.BookDetails.First(i => i.BookDetailId == BookId);
                obj.BookDetails.Remove(oSelect);

                return obj.SaveChanges();
            }
        }


No comments:

Post a Comment