public IActionResult Index() { db.Database.Migrate(); var malinfos = db.MalInfos.Where(d => d.DistributionId.HasValue == false).ToList(); var distributions = db.Distributions .Where(d => !db.MalInfos.Where(m => m.DistributionId.HasValue == true) .Select(m => m.DistributionId).Contains(d.Id)).ToList(); var joined = malinfos.Join(distributions, infos => infos.NameDamdar.Trim(), distribution => distribution.NamVaNameKhanevadegi.Trim(), (infos, distribution) => new { infos, distribution }).ToList(); foreach (var ajoin in joined) { if (ajoin.infos.DistributionId.HasValue == false) { ajoin.infos.DistributionId = ajoin.distribution.Id; db.Entry(ajoin.infos).Property(p => p.DistributionId).IsModified = true; } } db.SaveChanges(); return(View((malinfos, distributions))); }
public void Save(List <T> models) { var entities = db.Set <T>(); foreach (var model in models.ToList()) { if (model.Id == 0) { entities.Add(model); } else { var entity = entities.FirstOrDefault(e => e.Id == model.Id); if (entity == null) { throw new Exception("رکورد یافت نشد"); } db.Entry(entity).CurrentValues.SetValues(model); } } db.SaveChanges(); }