public void Remove(LookUpTitles lookuptitles)
 {
     if (Context.Entry(lookuptitles).State == EntityState.Detached)
     {
         context.LookUpTitles.Attach(lookuptitles);
     }
     context.LookUpTitles.Remove(lookuptitles);
 }
 public DbEntityEntry <LookUpTitles> Entry(LookUpTitles lookuptitles)
 {
     return(Context.Entry(lookuptitles));
 }
 public void Add(LookUpTitles lookuptitles)
 {
     context.LookUpTitles.Add(lookuptitles);
 }
 public void Attach(LookUpTitles lookuptitles)
 {
     context.LookUpTitles.Attach(lookuptitles);
 }
        public void UpdateLookUpCodes(IEnumerable <LookupCodesViewModel> models, string culture, string name)
        {
            if (models == null)
            {
                return;
            }
            var codeName = models.First().CodeName;
            var db       = context.LookUpCodes.ToList();
            var titiles  = context.LookUpTitles.Where(a => a.CodeName == codeName && a.Culture == culture).ToList();

            foreach (var model in models)
            {
                var record = db.FirstOrDefault(a => a.Id == model.Id);
                var title  = titiles.FirstOrDefault(a => a.CodeId == model.CodeId);

                if (record == null)
                {
                    //insert
                    var UserCode = new LookUpCode
                    {
                        CodeId      = model.CodeId,
                        Name        = model.Name,
                        Description = model.Description,
                        EndDate     = model.EndDate,
                        CreatedUser = name,
                        StartDate   = model.StartDate,
                        CodeName    = model.CodeName,
                        CreatedTime = DateTime.Now
                    };
                    context.LookUpCodes.Add(UserCode);
                }
                //Update
                else
                {
                    record.Name         = model.Name;
                    record.ModifiedTime = DateTime.Now;
                    record.ModifiedUser = name;
                    record.StartDate    = model.StartDate;
                    record.EndDate      = model.EndDate;
                    record.Description  = model.Description;
                    record.CodeId       = model.CodeId;
                    record.CodeName     = model.CodeName;
                    context.LookUpCodes.Attach(record);
                    context.Entry(record).State = EntityState.Modified;
                }
                //  if title changed update && insert
                if (title != null)
                {
                    title.CodeName = model.CodeName;
                    title.Name     = model.Name;
                    title.Title    = model.Title;
                    context.LookUpTitles.Attach(title);
                    context.Entry(title).State = EntityState.Modified;
                }
                else
                {
                    var Lookuptitle = new LookUpTitles
                    {
                        Culture  = culture,
                        CodeName = model.CodeName,
                        Name     = model.Name,
                        Title    = model.Title,
                        CodeId   = model.CodeId
                    };
                    context.LookUpTitles.Add(Lookuptitle);
                }
            }
        }