コード例 #1
0
ファイル: MSet.cs プロジェクト: sizzles/ecsharp
 /// <summary>Adds the specified item to the set, or throws an exception if
 /// a matching item is already present.</summary>
 /// <exception cref="ArgumentException">The item already exists in the set.</exception>
 public void AddUnique(T item)
 {
     if (_set.Add(ref item, _comparer, false))
     {
         _count++;
     }
     throw new ArgumentException("The item already exists in the set.");
 }
コード例 #2
0
 /// <summary>Adds the specified item to the set, or throws an exception if
 /// a matching item is already present.</summary>
 /// <exception cref="ArgumentException">The item already exists in the set.</exception>
 public void AddUnique(T item)
 {
     if (_set.Add(ref item, _comparer, false))
     {
         _count++;
     }
     CheckParam.ThrowBadArgument("The item already exists in the set.");
 }
コード例 #3
0
        /// <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);
        }
コード例 #4
0
        /// <inheritdoc/>
        public TEntity Add(TEntity entity)
        {
            Check.NotNull(entity, "entity");

            _internalSet.Add(entity);
            return(entity);
        }
コード例 #5
0
        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);
                }
            }
        }
コード例 #6
0
        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);
                }
            }
        }