public void Handle(IEnumerable <ITrackedEntry> changedEntries)
        {
            var versionId       = Guid.NewGuid();
            var _changedEntries = changedEntries.Where(i => (i.State == EntityState.Added || i.State == EntityState.Modified) &&
                                                       typeof(IVersionedEntity).IsAssignableFrom(i.Entity.GetType()))
                                  .ToArray();

            foreach (var entry in _changedEntries)
            {
                var versionedEntity = (IVersionedEntity)entry.Entity;
                if (entry.State == EntityState.Added)
                {
                    versionedEntity.VersionId = Guid.NewGuid();
                }
                else if (entry.State == EntityState.Modified)
                {
                    var entityType = versionedEntity.GetType();

                    /// Clone entity
                    var originalValues = entry.OriginalValues;
                    var originalEntity = (IVersionedEntity)originalValues.ToObject();

                    /// Assign new primary key
                    var property = entityType.GetProperty(EntityUtility.GetIdPropertyName(entityType));
                    property.SetValue(originalEntity, Guid.NewGuid());

                    /// Assign version
                    originalEntity.VersionId          = versionId;
                    versionedEntity.PreviousVersionId = originalEntity.VersionId;

                    context.Add(originalEntity);
                }
            }
        }
        internal static Expression <Func <TEntity, bool> > GetIdExpression(Guid id)
        {
            var parameterExpression = Expression.Parameter(typeof(TEntity));

            return(Expression.Lambda <Func <TEntity, bool> >(
                       Expression.Equal(
                           Expression.PropertyOrField(parameterExpression, EntityUtility.GetIdPropertyName(typeof(TEntity))),
                           Expression.Constant(id, typeof(Guid)))
                       , parameterExpression
                       ));
        }
        public virtual TEntity GetById <TId, TProperty>(TId id, params Expression <Func <TEntity, TProperty> >[] includes)
        {
            var query      = GetQuery(includes);
            var entityType = typeof(TEntity);
            var parameter  = Expression.Parameter(entityType);
            var left       = Expression.Property(parameter, EntityUtility.GetIdPropertyName(entityType));
            var right      = Expression.Constant(id);
            var body       = Expression.Equal(left, right);
            var predicate  = Expression.Lambda <Func <TEntity, bool> >(body, parameter);

            return(query.SingleOrDefault(predicate));
        }
Example #4
0
 private void InterceptPrimaryKey(DbEntityEntry entityEntry)
 {
     if (entityEntry.State == EntityState.Added)
     {
         var entity     = entityEntry.Entity;
         var entityType = entity.GetType();
         var property   = entityType.GetProperty(EntityUtility.GetIdPropertyName(entityType));
         var value      = property.GetValue(entity);
         if (value.GetType() == typeof(Guid))
         {
             var guid = (Guid)value;
             if (guid == Guid.Empty)
             {
                 property.SetValue(entity, Guid.NewGuid());
             }
         }
     }
 }