예제 #1
0
 public virtual void Delete(TE entity)
 {
     if (Context.Entry(entity).State == EntityState.Detached)
     {
         DbSet.Attach(entity);
     }
     DbSet.Remove(entity);
 }
예제 #2
0
        public virtual void Delete(TEntity entityToDelete)
        {
            if (Context.Entry(entityToDelete).State == EntityState.Detached)
            {
                Entities.Attach(entityToDelete);
            }

            Entities.Remove(entityToDelete);
        }
예제 #3
0
        public void Remove(TModel entity)
        {
            if (_dbContext.Entry(entity).State == EntityState.Detached)
            {
                ModelDbSets.Attach(entity);
            }

            ModelDbSets.Remove(entity);
        }
예제 #4
0
        public TEntity Update(TEntity entity)
        {
            if (entity == null)
            {
                return(null);
            }

            m_context.Set <TEntity>().Attach(entity);
            m_context.Entry(entity).State = EntityState.Modified;
            m_context.SaveChanges();
            return(entity);
        }
예제 #5
0
        public static void AddOrUpdate(this Microsoft.EntityFrameworkCore.DbContext ctx, object entity)
        {
            var entry = ctx.Entry(entity);

            switch (entry.State)
            {
            case EntityState.Detached:
                ctx.Add(entity);
                break;

            case EntityState.Modified:
                ctx.Update(entity);
                break;

            case EntityState.Added:
                ctx.Add(entity);
                break;

            case EntityState.Unchanged:
                //item already in db no need to do anything
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
예제 #6
0
        public void Update(TEnity item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            _context.Entry(item).State = EntityState.Modified;
        }
예제 #7
0
        public virtual EntityEntry <TEntity> Entry <TEntity>(TEntity entity) where TEntity : class
        {
            var internalEntry =
                ((IDbContextDependencies)_dbContext).StateManager.Entries.FirstOrDefault(ee => ee.Entity == entity);

            if (internalEntry != null)
            {
                return(new EntityEntry <TEntity>(internalEntry));
            }

            return(_dbContext.Entry(entity));
        }
예제 #8
0
        /// <summary>
        /// Checks dynamically if the entity exists in the databse. If it does, changes the referenced entity to the database entity, plus with it's values changed using the original entity.
        /// If the entity exists, won't change primary keys, navigation properties, properties with private getters and foreign keys ids in wich the given values are equal to 0.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="context"></param>
        /// <param name="expression"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        private static bool EntityExists <TEntity>(DbContext context, Expression <Func <TEntity, object> > expression, ref TEntity entity)
            where TEntity : class
        {
            var t = typeof(TEntity);

            var filterProperties =
                expression.Compile()(entity)
                .GetType().GetProperties().Select(p => t.GetProperty(p.Name)).ToList();

            if (filterProperties == null || filterProperties.Count == 0)
            {
                throw new Exception($"{t.FullName} does not have a KeyAttribute field. Unable to exec AddOrUpdate call.");
            }

            int?ct = null;

            int GenerateIndex()
            {
                if (ct == null)
                {
                    ct = 0;
                    return(ct.Value);
                }
                else
                {
                    ct++;
                }

                return(ct.Value);
            }

            var _entity = entity;

            var namesAndValuesToFilter = filterProperties
                                         .Select(x => new
            {
                Index = GenerateIndex(),
                x.Name,
                Value = x.GetValue(_entity)
            })
                                         .ToList();

            var expressions = string.Join(" AND ", namesAndValuesToFilter.OrderBy(x => x.Index).Select(x => $"{x.Name} == @{x.Index}"));

            var values = namesAndValuesToFilter.OrderBy(x => x.Index).Select(x => x.Value).ToArray();

            var entityType = context.Model.FindEntityType(typeof(TEntity));

            var pkKeyName = entityType.FindPrimaryKey().Properties
                            .Select(x => x.Name).FirstOrDefault() ?? "Id";

            var queryResult = context.Set <TEntity>().AsNoTracking()
                              .Where(expressions, values)
                              .Select($"new ({pkKeyName})")
                              .FirstOrDefault();

            if (queryResult != null)
            {
                // Gets the original Entity from the database and change only simple fields.
                var pksAndNavigations = entityType
                                        .FindPrimaryKey().Properties.Select(x => x.Name).Distinct().ToList();

                var fks = entityType
                          .GetForeignKeys().SelectMany(x => x.Properties.Select(p => p.Name)).Distinct().ToList();

                pksAndNavigations.AddRange(entityType
                                           .GetNavigations().Select(x => x.Name).Distinct().ToList());

                var propsToChange = t.GetProperties()
                                    .Where(x => !pksAndNavigations.Contains(x.Name) && x.CanWrite)
                                    .ToList();

                var fullEntity = context.Set <TEntity>()
                                 .Where(expressions, values)
                                 .FirstOrDefault();

                var dynamicChangeableFields = new ExpandoObject() as IDictionary <string, object>;

                foreach (var prop in propsToChange)
                {
                    var value = t.GetProperty(prop.Name).GetValue(entity, null);

                    // Allow to change only not null or FK values only if they are not 0.
                    if (value == null || (fks.Contains(prop.Name) && value is int && Convert.ToInt32(value) == 0))
                    {
                        continue;
                    }

                    dynamicChangeableFields.Add(prop.Name, value);
                }

                context.Entry(fullEntity).CurrentValues.SetValues(dynamicChangeableFields as ExpandoObject);
                context.Entry(fullEntity).State = EntityState.Modified;

                entity = fullEntity;

                return(true);
            }
            else
            {
                return(false);
            }
        }
 public static void ForceUpdate <TEntity>([NotNull] this SystemDbContext thisValue, [NotNull] TEntity entity)
     where TEntity : class
 {
     thisValue.Entry(entity).State = EntityState.Modified;
 }
 public static void ForceRefresh <TEntity>([NotNull] this SystemDbContext thisValue, [NotNull] TEntity entity)
     where TEntity : class
 {
     thisValue.Entry(entity).State = EntityState.Detached;
 }
 public static void ReloadNavigationProperty <TEntity, TElement>([NotNull] this SystemDbContext thisValue, [NotNull] TEntity entity, [NotNull] Expression <Func <TEntity, IEnumerable <TElement> > > navigationProperty)
     where TEntity : class
     where TElement : class
 {
     thisValue.Entry(entity).Collection(navigationProperty).Query();
 }
 public static Task ReloadAsync <TEntity>([NotNull] this SystemDbContext thisValue, [NotNull] TEntity entity, CancellationToken cancellationToken)
     where TEntity : class
 {
     return(thisValue.Entry(entity).ReloadAsync(cancellationToken));
 }
 public static void Reload <TEntity>([NotNull] this SystemDbContext thisValue, [NotNull] TEntity entity)
     where TEntity : class
 {
     thisValue.Entry(entity).Reload();
 }
        public async Task Can_add_update_delete_end_to_end_using_partial_shadow_state()
        {
            var model = new Model();

            var customerType = model.AddEntityType(typeof(Customer));
            var property1    = customerType.AddProperty("Id", typeof(int));

            customerType.GetOrSetPrimaryKey(property1);
            customerType.AddProperty("Name", typeof(string));

            var optionsBuilder = new DbContextOptionsBuilder()
                                 .UseModel(model)
                                 .UseInMemoryDatabase(nameof(ShadowStateUpdateTest))
                                 .UseInternalServiceProvider(_fixture.ServiceProvider);

            var customer = new Customer
            {
                Id = 42
            };

            using (var context = new DbContext(optionsBuilder.Options))
            {
                context.Add(customer);

                context.Entry(customer).Property("Name").CurrentValue = "Daenerys";

                await context.SaveChangesAsync();

                context.Entry(customer).Property("Name").CurrentValue = "Changed!";
            }

            using (var context = new DbContext(optionsBuilder.Options))
            {
                var customerFromStore = context.Set <Customer>().Single();

                Assert.Equal(42, customerFromStore.Id);
                Assert.Equal(
                    "Daenerys",
                    (string)context.Entry(customerFromStore).Property("Name").CurrentValue);
            }

            using (var context = new DbContext(optionsBuilder.Options))
            {
                var customerEntry = context.Entry(customer).GetInfrastructure();
                customerEntry[customerType.FindProperty("Name")] = "Daenerys Targaryen";

                context.Update(customer);

                await context.SaveChangesAsync();
            }

            using (var context = new DbContext(optionsBuilder.Options))
            {
                var customerFromStore = context.Set <Customer>().Single();

                Assert.Equal(42, customerFromStore.Id);
                Assert.Equal(
                    "Daenerys Targaryen",
                    (string)context.Entry(customerFromStore).Property("Name").CurrentValue);
            }

            using (var context = new DbContext(optionsBuilder.Options))
            {
                context.Remove(customer);

                await context.SaveChangesAsync();
            }

            using (var context = new DbContext(optionsBuilder.Options))
            {
                Assert.Equal(0, context.Set <Customer>().Count());
            }
        }