/// <summary> /// Marks the given entity as Deleted such that it will be deleted from the database when SaveChanges /// is called. Note that the entity must exist in the context in some other state before this method /// is called. /// </summary> /// <param name="entity"> The entity to remove. </param> /// <returns> The entity. </returns> /// <remarks> /// Note that if the entity exists in the context in the Added state, then this method /// will cause it to be detached from the context. This is because an Added entity is assumed not to /// exist in the database such that trying to delete it does not make sense. /// </remarks> public object Remove(object entity) { Check.NotNull(entity, "entity"); InternalSet.Remove(entity); return(entity); }
internal MSet <T> Xor(InternalSet <T> other) { var set2 = _set.CloneFreeze(); int count2 = _count + set2.SymmetricExceptWith(other, Comparer); return(new MSet <T>(set2, _comparer, count2)); }
/// <summary> /// Adds the given entity to the context underlying the set in the Added state such that it will /// be inserted into the database when SaveChanges is called. /// </summary> /// <param name="entity"> The entity to add. </param> /// <returns> The entity. </returns> /// <remarks> /// Note that entities that are already in the context in some other state will have their state set /// to Added. Add is a no-op if the entity is already in the context in the Added state. /// </remarks> public object Add(object entity) { Check.NotNull(entity, "entity"); InternalSet.Add(entity); return(entity); }
internal MSet <T> Intersect(InternalSet <T> other, IEqualityComparer <T> otherComparer) { var set2 = _set.CloneFreeze(); int count2 = _count - set2.IntersectWith(other, otherComparer); return(new MSet <T>(set2, Comparer, count2)); }
internal MSet <T> Except(InternalSet <T> other) { var set2 = _set.CloneFreeze(); int count2 = _count - set2.ExceptWith(other, Comparer); return(new MSet <T>(set2, _comparer, count2)); }
/// <summary> /// Creates a new set that will be backed by the given <see cref="InternalSet{T}" />. /// </summary> /// <param name="internalSet"> The internal set. </param> internal DbSet(InternalSet <TEntity> internalSet) : base(internalSet) { DebugCheck.NotNull(internalSet); _internalSet = internalSet; }
internal MSet <T> Union(InternalSet <T> other, bool replaceWithValuesFromOther = false) { var set2 = _set.CloneFreeze(); int count2 = _count + set2.UnionWith(other, Comparer, replaceWithValuesFromOther); return(new MSet <T>(set2, _comparer, count2)); }
internal MapOrMMap(InternalSet <KeyValuePair <K, V> > set, IEqualityComparer <K> keyComparer, int count) { _set = set; _keyComparer = keyComparer; _count = count; _set.CloneFreeze(); }
private static IEnumerable <PropertyPath> GetKeyProperties <TEntity>( Type entityType, InternalSet <TEntity> internalSet) where TEntity : class { return(internalSet.InternalContext.GetEntitySetAndBaseTypeForType(typeof(TEntity)).EntitySet.ElementType.KeyMembers.Select <EdmMember, PropertyPath>((Func <EdmMember, PropertyPath>)(km => new PropertyPath(entityType.GetAnyProperty(km.Name))))); }
/// <summary> /// Adds or updates entities by key when SaveChanges is called. Equivalent to an "upsert" operation /// from database terminology. /// This method can useful when seeding data using Migrations. /// </summary> /// <typeparam name="TEntity">The type of entities to add or update.</typeparam> /// <param name="set">The set to which the entities belong.</param> /// <param name="entities"> The entities to add or update. </param> /// <remarks> /// When the <paramref name="set" /> parameter is a custom or fake IDbSet implementation, this method will /// attempt to locate and invoke a public, instance method with the same signature as this extension method. /// </remarks> public static void AddOrUpdate <TEntity>(this IDbSet <TEntity> set, params TEntity[] entities) where TEntity : class { Check.NotNull <IDbSet <TEntity> >(set, nameof(set)); Check.NotNull <TEntity[]>(entities, nameof(entities)); DbSet <TEntity> set1 = set as DbSet <TEntity>; if (set1 != null) { InternalSet <TEntity> internalSet = (InternalSet <TEntity>)((IInternalSetAdapter)set1).InternalSet; if (internalSet != null) { set1.AddOrUpdate <TEntity>(DbSetMigrationsExtensions.GetKeyProperties <TEntity>(typeof(TEntity), internalSet), internalSet, entities); return; } } Type type = set.GetType(); MethodInfo declaredMethod = type.GetDeclaredMethod(nameof(AddOrUpdate), typeof(TEntity[])); if (declaredMethod == (MethodInfo)null) { throw Error.UnableToDispatchAddOrUpdate((object)type); } declaredMethod.Invoke((object)set, (object[])new TEntity[1][] { entities }); }
/// <summary> /// Removes the given collection of entities from the context underlying the set with each entity being put into /// the Deleted state such that it will be deleted from the database when SaveChanges is called. /// </summary> /// <param name="entities">The collection of entities to delete.</param> /// <returns> /// The collection of entities. /// </returns> /// <remarks> /// Note that if <see cref="DbContextConfiguration.AutoDetectChangesEnabled" /> is set to true (which is /// the default), then DetectChanges will be called once before delete any entities and will not be called /// again. This means that in some situations RemoveRange may perform significantly better than calling /// Remove multiple times would do. /// Note that if any entity exists in the context in the Added state, then this method /// will cause it to be detached from the context. This is because an Added entity is assumed not to /// exist in the database such that trying to delete it does not make sense. /// </remarks> public IEnumerable RemoveRange(IEnumerable entities) { Check.NotNull(entities, "entities"); InternalSet.RemoveRange(entities); return(entities); }
public static void AddOrUpdate <TEntity>( this IDbSet <TEntity> set, Expression <Func <TEntity, object> > identifierExpression, params TEntity[] entities) where TEntity : class { Check.NotNull <IDbSet <TEntity> >(set, nameof(set)); Check.NotNull <Expression <Func <TEntity, object> > >(identifierExpression, nameof(identifierExpression)); Check.NotNull <TEntity[]>(entities, nameof(entities)); DbSet <TEntity> set1 = set as DbSet <TEntity>; if (set1 != null) { InternalSet <TEntity> internalSet = (InternalSet <TEntity>)((IInternalSetAdapter)set1).InternalSet; if (internalSet != null) { IEnumerable <PropertyPath> propertyAccessList = identifierExpression.GetSimplePropertyAccessList(); set1.AddOrUpdate <TEntity>(propertyAccessList, internalSet, entities); return; } } Type type = set.GetType(); MethodInfo declaredMethod = type.GetDeclaredMethod(nameof(AddOrUpdate), typeof(Expression <Func <TEntity, object> >), typeof(TEntity[])); if (declaredMethod == (MethodInfo)null) { throw Error.UnableToDispatchAddOrUpdate((object)type); } declaredMethod.Invoke((object)set, new object[2] { (object)identifierExpression, (object)entities }); }
internal Set <T> Union(InternalSet <T> other, bool replaceWithValuesFromOther = false) { Debug.Assert(_set.IsRootFrozen); var set2 = _set; int count2 = _count + set2.UnionWith(other, Comparer, replaceWithValuesFromOther); return(new Set <T>(set2, _comparer, count2)); }
internal Set <T> Intersect(InternalSet <T> other, IEqualityComparer <T> otherComparer) { Debug.Assert(_set.IsRootFrozen); var set2 = _set; int count2 = _count - set2.IntersectWith(other, otherComparer); return(new Set <T>(set2, Comparer, count2)); }
internal Set <T> Xor(InternalSet <T> other) { Debug.Assert(_set.IsRootFrozen); var set = _set; int count2 = _count + set.SymmetricExceptWith(other, Comparer); return(new Set <T>(set, _comparer, count2)); }
internal Set <T> Except(InternalSet <T> other) { Debug.Assert(_set.IsRootFrozen); var set2 = _set; int count2 = _count - set2.ExceptWith(other, Comparer); return(new Set <T>(set2, _comparer, count2)); }
internal Set(InternalSet <T> set, IEqualityComparer <T> comparer, int count) { Debug.Assert(count >= 0); _set = set; _comparer = comparer; _count = count; set.CloneFreeze(); }
public void OperationCanceledException_is_thrown_if_task_is_cancelled() { var internalSet = new InternalSet <FakeEntity>(new Mock <InternalContext>().Object); var dbSet = new DbSet <FakeEntity>(internalSet); Assert.Throws <OperationCanceledException>( () => dbSet.FindAsync(new CancellationToken(canceled: true)) .GetAwaiter().GetResult()); }
private static IEnumerable <PropertyPath> GetKeyProperties <TEntity>( Type entityType, InternalSet <TEntity> internalSet) where TEntity : class { DebugCheck.NotNull(entityType); DebugCheck.NotNull(internalSet); return(internalSet.InternalContext .GetEntitySetAndBaseTypeForType(typeof(TEntity)) .EntitySet.ElementType.KeyMembers .Select(km => new PropertyPath(entityType.GetAnyProperty(km.Name)))); }
public Set(IEnumerable <T> list, IEqualityComparer <T> comparer) { _set = new InternalSet <T>(); _comparer = comparer; _count = 0; if (list != null) { _count = _set.UnionWith(list, comparer, false); _set.CloneFreeze(); } if (comparer == null && !_set.HasRoot) { // give it a root so that Comparer does not change _comparer _set = InternalSet <T> .Empty; } }
private static void AddOrUpdate <TEntity>( this DbSet <TEntity> set, IEnumerable <PropertyPath> identifyingProperties, InternalSet <TEntity> internalSet, params TEntity[] entities) where TEntity : class { DebugCheck.NotNull(set); DebugCheck.NotNull(identifyingProperties); DebugCheck.NotNull(entities); var keyProperties = GetKeyProperties(typeof(TEntity), internalSet); var parameter = Expression.Parameter(typeof(TEntity)); foreach (var entity in entities) { var matchExpression = identifyingProperties.Select( pi => Expression.Equal( Expression.Property(parameter, pi.Single()), Expression.Constant(pi.Last().GetValue(entity, null), pi.Last().PropertyType))) .Aggregate <BinaryExpression, Expression>( null, (current, predicate) => (current == null) ? predicate : Expression.AndAlso(current, predicate)); var existing = set.SingleOrDefault(Expression.Lambda <Func <TEntity, bool> >(matchExpression, new[] { parameter })); if (existing != null) { foreach (var keyProperty in keyProperties) { keyProperty.Single().GetPropertyInfoForSet().SetValue(entity, keyProperty.Single().GetValue(existing, null), null); } internalSet.InternalContext.Owner.Entry(existing).CurrentValues.SetValues(entity); } else { internalSet.Add(entity); } } }
private static void AddOrUpdate <TEntity>( this DbSet <TEntity> set, IEnumerable <PropertyPath> identifyingProperties, InternalSet <TEntity> internalSet, params TEntity[] entities) where TEntity : class { IEnumerable <PropertyPath> keyProperties = DbSetMigrationsExtensions.GetKeyProperties <TEntity>(typeof(TEntity), internalSet); ParameterExpression parameter = Expression.Parameter(typeof(TEntity)); foreach (TEntity entity1 in entities) { TEntity entity = entity1; Expression body = identifyingProperties.Select <PropertyPath, BinaryExpression>((Func <PropertyPath, BinaryExpression>)(pi => Expression.Equal((Expression)Expression.Property((Expression)parameter, pi.Single <PropertyInfo>()), (Expression)Expression.Constant(pi.Last <PropertyInfo>().GetValue((object)entity, (object[])null))))).Aggregate <BinaryExpression, Expression>((Expression)null, (Func <Expression, BinaryExpression, Expression>)((current, predicate) => { if (current != null) { return((Expression)Expression.AndAlso(current, (Expression)predicate)); } return((Expression)predicate); })); TEntity entity2 = set.SingleOrDefault <TEntity>(Expression.Lambda <Func <TEntity, bool> >(body, parameter)); if ((object)entity2 != null) { foreach (PropertyPath source in keyProperties) { source.Single <PropertyInfo>().GetPropertyInfoForSet().SetValue((object)(TEntity)entity, source.Single <PropertyInfo>().GetValue((object)entity2, (object[])null), (object[])null); } internalSet.InternalContext.Owner.Entry <TEntity>(entity2).CurrentValues.SetValues((object)(TEntity)entity); } else { internalSet.Add((object)(TEntity)entity); } } }
internal DbSet(InternalSet <TEntity> internalSet) : base((IInternalQuery <TEntity>)internalSet) { this._internalSet = internalSet; }
public Task RemoveLogs(long shortUrlId) { InternalSet.RemoveRange(InternalSet.Where(x => x.ShortUrlId == shortUrlId)); return(Save()); }
internal DbSet(DbContext owner) { Check.NotNull(owner, "context"); _internalSet = new InternalSet <TEntity>(owner); }
protected MapOrMMap(IEnumerable <KeyValuePair <K, V> > list, IEqualityComparer <K> comparer) { _keyComparer = comparer ?? ValueComparer <K> .Default; _set = new InternalSet <KeyValuePair <K, V> >(list, this, out _count); }
internal Map(InternalSet <KeyValuePair <K, V> > set, IEqualityComparer <K> keyComparer, int count) : base(set, keyComparer, count) { }
// <summary> // Creates a new set that will be backed by the given <see cref="InternalSet{T}" />. // </summary> // <param name="internalSet"> The internal set. </param> internal DbSet(InternalSet <TEntity> internalSet) : base(internalSet) { _internalSet = internalSet; }
internal Enumerator(InternalSet <T> set) { _e = new InternalSet <T> .Enumerator(set); }
public Set(InternalSet <T> set, IEqualityComparer <T> comparer) : this(set, comparer, set.Count()) { }