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

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();         }     }

Custom calendar in asp.net

<form id="form1" runat="server">     <div>         <asp:DropDownList ID="ddlMonth" runat="server" OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged"             AutoPostBack="true">             <asp:ListItem Text="Jan" Value="1"></asp:ListItem>             <asp:ListItem Text="Feb" Value="2"></asp:ListItem>             <asp:ListItem Text="Mar" Value="3"></asp:ListItem>             <asp:ListItem Text="Apr" Value="4"></asp:ListItem>             <asp:ListItem Text="May" Value="5"></asp:ListItem>       ...