Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts

Tuesday, April 8, 2014

Get Data Using Entity Framework

  1. Use the following method as reference to get the data from Database.

    public List<tbl_M_EventLog> GetEventLog()
            {
                List<tbl_M_EventLog> objEventLog = null;
                try
                {
                    objEventLog = DBConnection.Entities.tbl_M_EventLog.OrderByDescending(T => T.EventLogId).ToList();
                }
                catch (Exception ex)
                {
                               }
                return objEventLog;
            }

Update Using Entity Framework

  1. Use the following method as a reference to update the record in Table using Entity Framework.

    public bool UpdateEventLog(tbl_M_EventLog objNewEventLog)
    {
        bool status = false;
        try
        {
        tbl_M_EventLog objEventLog = DBConnection.Entities.tbl_M_EventLog.SingleOrDefault(p => p.EventId == objNewEventLog.EventId);
        DBConnection.Entities.ObjectStateManager.ChangeObjectState(objEventLog, System.Data.EntityState.Modified);
        DBCommon.ReflectSingleData(objEventLog, objNewEventLog, true);
        DBConnection.Entities.SaveChanges();
        DBConnection.SaveChanges();
        status = true;
        }
        catch (Exception ex)
        {
       
        }
        return status;
    }

Insert Using Entity Framework

  1. Use the following method as a reference to insert a new record in Table.

    public long InsertEventLog(tbl_M_EventLog objEventLog)
    {
        long EventId=0;
        try
        {
        DBConnection.Entities.AddTotbl_M_EventLog(objEventLog);
        DBConnection.SaveChanges();
        EventId = objEventLog.EventId;
        }
        catch (Exception ex)
        {
       
        }
        return EventId;
    }

     

Create Using Entity Framework in Asp .Net





Please follow the below steps to create Entities.

  1. Right click on Solution Explorer. And Click on Add New Item. Then Select ADO.NET Entity Data Model.
  2. Then select Generate Data From Database.
  3. Then choose your database Objects to create the entities.
  4. Then you will see the selected objects on the Entity file.