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

QueryString Encode & Decode

QueryString Encode & Decode Helper Class public class clsQueryString { static public string QueryStringEncode( string value) { return clsEncryptionDecryption .GetEncrypt(value); } static public string QueryStringDecode( string value) { return clsEncryptionDecryption .GetDecrypt(value); } public static string GetValueFromQueryString( string value, string key) { string strValue = string .Empty; if (value != null ) { string DataString = clsEncryptionDecryption .GetDecrypt(value); Hashtable objHash = clsQueryString .GetQueryString(DataString); strValue = clsCheckDBNull .ConvertToStr(objHash[key]); return strValue; } else return "" ; } publi...