Skip to main content

CRUD in EF

 public int Create(Users entity)
        {
            using (var context = new K305DataEntities())
            {
                var newEntity = Mapper.Map<User>(entity);
           
                context.Users.Add(newEntity);

                context.SaveChanges();

                return newEntity.Id;
            }
        }
        public int Update(Users entity)
        {
            using (var context = new K305DataEntities())
            {
                var dbEntity = context.Users.FirstOrDefault(dc => dc.Id == entity.Id);
                if (dbEntity == null) throw new ApplicationException("Entity not found.");

                dbEntity.Name = entity.Name;

                context.SaveChanges();

                return dbEntity.Id;
            }        
        }

        public List<Users> GetAll()
        {
            using (var context = new K305DataEntities())
            {
                return context.Users.Select(
                    rhf =>
                        new Users
                        {
                            Id = rhf.Id,
                            Name = rhf.Name,
                            EmailId = rhf.EmailId,
                            BirthDate = rhf.BirthDate,
                            IsActive = rhf.IsActive
                        }).OrderBy(o => o.Name).ToList();
            }
        }

        public Users GetById(int id)
        {
            using (var context = new K305DataEntities())
            {
                return context.Users.Where(u => u.Id == id).Select(
                    rhf =>
                        new Users
                        {
                            Id = rhf.Id,
                            Name = rhf.Name,
                            EmailId = rhf.EmailId,
                            BirthDate = rhf.BirthDate,
                            IsActive = rhf.IsActive
                        }).FirstOrDefault();
            }
        }

        public void Delete(Users entity)
        {
            using (var context = new K305DataEntities())
            {
                var old = context.Users.FirstOrDefault(t => t.Id == entity.Id);
                context.Users.Remove(old);

                context.SaveChanges();
            }
        }

Comments

Popular posts from this blog

Tata Punch: A Compact SUV That Packs a Punch

Message Helper for MVC Application

Message Helper for MVC Application public static class MessageExtensions { public enum MessageType { Success = 0, Info = 1, Warning = 2, Error = 3 } public static void ShowMessage( this Controller controller, MessageType messageType, string message, bool showAfterRedirect = false ) { string messageTypeKey = messageType.ToString(); if (showAfterRedirect) { controller.TempData[messageTypeKey] = message; } else { controller.ViewData[messageTypeKey] = message; } } public static HtmlString RenderMessages( this HtmlHelper htmlHelper, bool clearMessage = true ) { string messages = String .Empty; foreach ( string messageType in Enum .GetNames( t...

Postback Page using Javascript in ASP.net

Postback Page using Javascript in ASP.net For Calling Function: __doPostBack('__Page', ''); Function: function __doPostBack(eventTarget, eventArgument) {         if (!theForm.onsubmit || (theForm.onsubmit() != false)) {             theForm.__EVENTTARGET.value = eventTarget;             theForm.__EVENTARGUMENT.value = eventArgument;             theForm.submit();         }     }