Esempio n. 1
0
        public virtual void LoadCollection <TProperty>(TEntity entity, Expression <Func <TEntity, IEnumerable <TProperty> > > childs)
            where TProperty : class
        {
            Attach(entity);

            CollectionEntry <TEntity, TProperty> collection = _dbContext.Entry(entity).Collection(childs);

            if (collection.IsLoaded == false)
            {
                collection.Load();
            }
        }
Esempio n. 2
0
 public static void Reload(this CollectionEntry source)
 {
     if (source.CurrentValue != null)
     {
         foreach (var item in source.CurrentValue)
         {
             source.EntityEntry.Context.Entry(item).State = EntityState.Detached;
         }
         source.CurrentValue = null;
     }
     source.IsLoaded = false;
     source.Load();
 }
Esempio n. 3
0
        public virtual void LoadCollection <TProperty>(TDto dto, Expression <Func <TDto, IEnumerable <TProperty> > > childs)
            where TProperty : class
        {
            try
            {
                Attach(dto);

                CollectionEntry <TDto, TProperty> collection = _dbContext.Entry(dto).Collection(childs);

                if (collection.IsLoaded == false)
                {
                    collection.Load();
                }
            }
            finally
            {
                Detach(dto);
            }
        }
Esempio n. 4
0
        private void ProcessEntry(CollectionEntry collectionEntry)
        {
            if (!collectionEntry.IsLoaded)
            {
                collectionEntry.Load();
            }

            if (collectionEntry.CurrentValue == null)
            {
                return;
            }

            var collection = new List <EntityEntry>();

            switch (collectionEntry.Metadata.ForeignKey.DeleteBehavior)
            {
            // We only have to process changes in database,
            // about changes on the client side the ef core will take care.
            case DeleteBehavior.SetNull:
                collection.AddRange(from object entity in collectionEntry.CurrentValue select Entry(entity));
                foreach (var dependentEntry in collection)
                {
                    SetNull(dependentEntry, collectionEntry.Metadata.ForeignKey);
                }
                break;

            case DeleteBehavior.Cascade:
                foreach (var entity in collectionEntry.CurrentValue)
                {
                    Remove(entity);
                }
                break;

                // Do nothing for other cases
            }
        }