//public async Task<T> GetAsync(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes)
        //{
        //    var result = await _objectSet.FirstOrDefaultAsync(predicate);
        //    return result;
        //}
        public async Task <T> UpdateAsync(T updated, int key)
        {
            if (updated == null)
            {
                return(null);
            }

            T existing = await _objectSet.FindAsync(key);

            var buildings    = _context.Building;
            var Newbuildings = buildings;

            if (existing != null)
            {
                _context.Entry(existing).State = EntityState.Modified;
                _context.Attach(existing);
                var entry      = _context.Entry(existing);
                var properties = existing.GetType().GetProperties();
                foreach (var property in properties)
                {
                    if (property.GetValue(updated) != null)
                    {
                        var newvalue = property.GetValue(updated);
                        property.SetValue(existing, newvalue);
                    }
                }
                entry.CurrentValues.SetValues(updated);
                await _context.SaveChangesAsync();
            }
            return(existing);
        }
Exemplo n.º 2
0
        public void CleanUp()
        {
            // All the data needs to be reverted back in the reverse order
            // of the order in which they are modified(i.e, SaveChangesAttempt) into DB
            // For the values modified in an instance, they should be undoed
            // as in the same order in which they are added in Context ChangeTracker
            foreach (var cleanUpDataInfo in CleanUpDataInfoes
                     .OrderBy(cudi => cudi.DateTime)//TODO: remove this condition?
                     .OrderByDescending(cudi => cudi.SaveChangesAttempt))
            {
                using (ApiContext context = dbContextFactory.Create())
                {
                    switch (cleanUpDataInfo.EntityState)
                    {
                    case EntityState.Modified:
                        context.Attach(cleanUpDataInfo.Entity);
                        context.Entry(cleanUpDataInfo.Entity).CurrentValues.SetValues(cleanUpDataInfo.Values);
                        break;

                    case EntityState.Added:
                        context.Remove(cleanUpDataInfo.Entity);
                        break;

                    case EntityState.Deleted:
                        string databaseName            = context.Database.GetDbConnection().Database;
                        var    mapping                 = context.Model.FindEntityType(cleanUpDataInfo.Entity.GetType()).Relational();
                        string schema                  = mapping.Schema ?? "dbo";
                        string tableName               = mapping.TableName;
                        string rawSqlIdentityInsertOn  = $"SET IDENTITY_INSERT [{databaseName}].[{schema}].[{tableName}] ON";
                        string rawSqlIdentityInsertOff = $"SET IDENTITY_INSERT [{databaseName}].[{schema}].[{tableName}] OFF";

                        // Data Fixup
                        var references = cleanUpDataInfo.EntityEntry.References;
                        foreach (ReferenceEntry referenceEntry in references)
                        {
                            // Set this to null, as a workaround for the scenario,
                            // when Navigation Property(User) value is passed along
                            // with it's corresponding field(UserId) value
                            referenceEntry.CurrentValue = null;
                        }

                        using (IDbContextTransaction transaction = context.Database.BeginTransaction())
                        {
                            //context.Database.ExecuteSqlCommand($"SET IDENTITY_INSERT [WebApiDatabase].[dbo].[Users] ON");
                            // Commented were not working
                            //context.Database.ExecuteSqlCommand($"SET IDENTITY_INSERT [{databaseName}].[{schema}].[{tableName}] ON");
                            //context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [{0}].[{1}].[{2}] ON", databaseName, schema, tableName);
                            context.Database.ExecuteSqlCommand(rawSqlIdentityInsertOn);

                            context.Add(cleanUpDataInfo.Entity);

                            context.SaveChanges();

                            context.Database.ExecuteSqlCommand(rawSqlIdentityInsertOff);

                            transaction.Commit();

                            continue;
                        }

                    default:
                        continue;
                        //break;
                    }

                    context.SaveChanges();
                }
            }
        }