public virtual void LoadReference <TProperty>(TEntity entity, Expression <Func <TEntity, TProperty> > member, bool forceReload = false) where TProperty : class { DbReferenceEntry <TEntity, TProperty> reference = _dbContext.Entry(entity).Reference(member); if (forceReload == true || reference.IsLoaded == false) { reference.Load(); } }
public void Load <TRelation>(Expression <Func <TEntity, TRelation> > accessor) where TRelation : class { DbEntityEntry <TEntity> entry = context.Entry(entity); DbReferenceEntry reference = entry.Reference(accessor); if (!reference.IsLoaded) { reference.Load(); } }
public virtual void LoadReference <TProperty>(TEntity entity, Expression <Func <TEntity, TProperty> > member) where TProperty : class { Attach(entity); DbReferenceEntry <TEntity, TProperty> reference = _dbContext.Entry(entity).Reference(member); if (reference.IsLoaded == false) { reference.Load(); } }
public override P AddToParent <P>(int parentId, T document) { //string sParentPropID = typeof(P).Name + "Key"; //P parent = context.Database.SqlQuery<P>("select * from " + typeof(P).Name + " where " + sParentPropID + " = @p0", parentId).FirstOrDefault(); DbSet <P> parentSet = context.Set <P>(); P parent = parentSet.Find(parentId); if (parent == null) { throw new Exception("Non-existent Parent Entity."); } if (parent is BaseDocument) { if ((parent as BaseDocument).sys_active == false) { throw new Exception("Non-existent Parent Entity."); } } //parentSet.Attach(parent); //context.Entry(parent).State = EntityState.Unchanged; string navigationPropertyName = typeof(T).Name; DbMemberEntry memberEntry; try { memberEntry = context.Entry(parent).Member(navigationPropertyName); } catch (Exception) { try { navigationPropertyName += "s"; memberEntry = context.Entry(parent).Member(navigationPropertyName); } catch (Exception) { throw new KnownError("Property not found: [" + navigationPropertyName + "]"); } } if (memberEntry is DbCollectionEntry) { DbCollectionEntry <P, T> childrenCollection = context.Entry(parent).Collection <T>(navigationPropertyName); childrenCollection.Load(); if (!childrenCollection.CurrentValue.Contains(document)) { childrenCollection.CurrentValue.Add(document); if (document.id > 0) { context.Entry(document).State = EntityState.Unchanged; document.InfoTrack = context.Set <Track>() .AsNoTracking() .FirstOrDefault(t => t.Entity_ID == document.id && t.Entity_Kind == document.AAA_EntityName); if (document.InfoTrack != null) { document.InfoTrack.User_LastEditedByKey = ByUserId; document.InfoTrack.Date_EditedOn = DateTimeOffset.Now; context.Entry(document.InfoTrack).State = EntityState.Modified; } } else { context.Entry(document).State = EntityState.Added; document.InfoTrack = new Track() { Date_CreatedOn = DateTimeOffset.Now, Entity_ID = document.id, Entity_Kind = document.AAA_EntityName, User_CreatedByKey = ByUserId, User_AssignedToKey = ByUserId }; context.Entry(document.InfoTrack).State = EntityState.Added; } context.SaveChanges(); } } else if (memberEntry is DbReferenceEntry) { DbReferenceEntry <P, T> childrenReference = context.Entry(parent).Reference <T>(navigationPropertyName); childrenReference.Load(); if (childrenReference.CurrentValue == null || !childrenReference.CurrentValue.Equals(document)) { childrenReference.CurrentValue = document; if (document.id > 0) { context.Entry(document).State = EntityState.Unchanged; document.InfoTrack = context.Set <Track>() .AsNoTracking() .FirstOrDefault(t => t.Entity_ID == document.id && t.Entity_Kind == document.AAA_EntityName); if (document.InfoTrack != null) { document.InfoTrack.User_LastEditedByKey = ByUserId; document.InfoTrack.Date_EditedOn = DateTimeOffset.Now; context.Entry(document.InfoTrack).State = EntityState.Modified; } } else { context.Entry(document).State = EntityState.Added; document.InfoTrack = new Track() { Date_CreatedOn = DateTimeOffset.Now, Entity_ID = document.id, Entity_Kind = document.AAA_EntityName, User_CreatedByKey = ByUserId, User_AssignedToKey = ByUserId }; context.Entry(document.InfoTrack).State = EntityState.Added; } context.SaveChanges(); } } return(parent); }
public P AddToParent <P>(int parentId, T entity) where P : class { //string sParentPropID = typeof(P).Name + "Key"; //P parent = context.Database.SqlQuery<P>("select * from " + typeof(P).Name + " where " + sParentPropID + " = @p0", parentId).FirstOrDefault(); DbSet <P> parentSet = context.Set <P>(); P parent = parentSet.Find(parentId); if (parent == null) { throw new Exception("Non-existent Parent Entity."); } if (parent is BaseDocument) { if ((parent as BaseDocument).sys_active == false) { throw new Exception("Non-existent Parent Entity."); } } //parentSet.Attach(parent); //context.Entry(parent).State = EntityState.Unchanged; string navigationPropertyName = typeof(T).Name; DbMemberEntry memberEntry; try { memberEntry = context.Entry(parent).Member(navigationPropertyName); } catch (Exception) { try { navigationPropertyName += "s"; memberEntry = context.Entry(parent).Member(navigationPropertyName); } catch (Exception) { throw new KnownError("Property not found: [" + navigationPropertyName + "]"); } } if (memberEntry is DbCollectionEntry) { DbCollectionEntry <P, T> childrenCollection = context.Entry(parent).Collection <T>(navigationPropertyName); childrenCollection.Load(); if (!childrenCollection.CurrentValue.Contains(entity)) { childrenCollection.CurrentValue.Add(entity); if (entity.id > 0) { context.Entry(entity).State = EntityState.Unchanged; /*DOCUMENT*/ if (typeof(T).IsSubclassOf(typeof(BaseDocument))) { var document = entity as BaseDocument; document.InfoTrack = context.Set <Track>() .AsNoTracking() .FirstOrDefault(t => t.Entity_ID == document.id && t.Entity_Kind == document.AAA_EntityName); if (document.InfoTrack != null) { document.InfoTrack.User_LastEditedByKey = byUserId; document.InfoTrack.Date_EditedOn = DateTime.Now; context.Entry(document.InfoTrack).State = EntityState.Modified; } } } else { context.Entry(entity).State = EntityState.Added; /*DOCUMENT*/ if (typeof(T).IsSubclassOf(typeof(BaseDocument))) { var document = entity as BaseDocument; document.InfoTrack = new Track(); //(entity as Trackable).InfoTrack = trackRepository.GetSingle(context, t => t.Entity_ID == entity.ID && t.Entity_Kind == entity.AAA_EntityName); document.InfoTrack.Date_CreatedOn = DateTime.Now; document.InfoTrack.Entity_ID = document.id; document.InfoTrack.Entity_Kind = document.AAA_EntityName; document.InfoTrack.User_CreatedByKey = byUserId; document.InfoTrack.User_AssignedToKey = byUserId; context.Entry(document.InfoTrack).State = EntityState.Added; } } context.SaveChanges(); } } else if (memberEntry is DbReferenceEntry) { DbReferenceEntry <P, T> childrenReference = context.Entry(parent).Reference <T>(navigationPropertyName); childrenReference.Load(); if (childrenReference.CurrentValue == null || !childrenReference.CurrentValue.Equals(entity)) { childrenReference.CurrentValue = entity; if (entity.id > 0) { context.Entry(entity).State = EntityState.Unchanged; /*DOCUMENT*/ if (typeof(T).IsSubclassOf(typeof(BaseDocument))) { var document = entity as BaseDocument; document.InfoTrack = context.Set <Track>() .AsNoTracking() .FirstOrDefault(t => t.Entity_ID == document.id && t.Entity_Kind == document.AAA_EntityName); if (document.InfoTrack != null) { document.InfoTrack.User_LastEditedByKey = byUserId; document.InfoTrack.Date_EditedOn = DateTime.Now; context.Entry(document.InfoTrack).State = EntityState.Modified; } } } else { context.Entry(entity).State = EntityState.Added; /*DOCUMENT*/ if (typeof(T).IsSubclassOf(typeof(BaseDocument))) { var document = entity as BaseDocument; document.InfoTrack = new Track(); //(entity as Trackable).InfoTrack = trackRepository.GetSingle(context, t => t.Entity_ID == entity.ID && t.Entity_Kind == entity.AAA_EntityName); document.InfoTrack.Date_CreatedOn = DateTime.Now; document.InfoTrack.Entity_ID = document.id; document.InfoTrack.Entity_Kind = document.AAA_EntityName; document.InfoTrack.User_CreatedByKey = byUserId; document.InfoTrack.User_AssignedToKey = byUserId; context.Entry(document.InfoTrack).State = EntityState.Added; } } context.SaveChanges(); } } //DbSet<T> entitySet = context.Set<T>(); //entitySet.Attach(entity); //PropertyInfo navigationProperty = parent.GetType().GetProperty(navigationPropertyName, BindingFlags.Public | BindingFlags.Instance); //ICollection<T> childrenList = (ICollection<T>)navigationProperty.GetValue(entity); //childrenList.Add(entity); return(parent); }
public virtual P AddToParent <P>(int parentId, T entity) where P : class { //string sParentPropID = typeof(P).Name + "Key"; //P parent = context.Database.SqlQuery<P>("select * from " + typeof(P).Name + " where " + sParentPropID + " = @p0", parentId).FirstOrDefault(); DbSet <P> parentSet = context.Set <P>(); P parent = parentSet.Find(parentId); if (parent == null) { throw new Exception("Non-existent Parent Entity."); } if (parent is BaseDocument) { if ((parent as BaseDocument).sys_active == false) { throw new Exception("Non-existent Parent Entity."); } } //parentSet.Attach(parent); //context.Entry(parent).State = EntityState.Unchanged; string navigationPropertyName = typeof(T).Name; DbMemberEntry memberEntry; try { memberEntry = context.Entry(parent).Member(navigationPropertyName); } catch (Exception) { try { navigationPropertyName += "s"; memberEntry = context.Entry(parent).Member(navigationPropertyName); } catch (Exception) { throw new KnownError("Property not found: [" + navigationPropertyName + "]"); } } if (memberEntry is DbCollectionEntry) { DbCollectionEntry <P, T> childrenCollection = context.Entry(parent).Collection <T>(navigationPropertyName); childrenCollection.Load(); if (!childrenCollection.CurrentValue.Contains(entity)) { childrenCollection.CurrentValue.Add(entity); if (entity.id > 0) { context.Entry(entity).State = EntityState.Unchanged; } else { context.Entry(entity).State = EntityState.Added; } context.SaveChanges(); } } else if (memberEntry is DbReferenceEntry) { DbReferenceEntry <P, T> childrenReference = context.Entry(parent).Reference <T>(navigationPropertyName); childrenReference.Load(); if (childrenReference.CurrentValue == null || !childrenReference.CurrentValue.Equals(entity)) { childrenReference.CurrentValue = entity; if (entity.id > 0) { context.Entry(entity).State = EntityState.Unchanged; } else { context.Entry(entity).State = EntityState.Added; } context.SaveChanges(); } } //DbSet<T> entitySet = context.Set<T>(); //entitySet.Attach(entity); //PropertyInfo navigationProperty = parent.GetType().GetProperty(navigationPropertyName, BindingFlags.Public | BindingFlags.Instance); //ICollection<T> childrenList = (ICollection<T>)navigationProperty.GetValue(entity); //childrenList.Add(entity); return(parent); }
public virtual TEntity Find(params object[] keyValues) { TEntity entity; if (((IEnumerable <Type>) typeof(TEntity).GetInterfaces()).Any <Type>((Func <Type, bool>)(t => t.FullName == typeof(ICacheable).FullName))) { List <TEntity> source = CacheManager.GetData(typeof(TEntity).FullName) as List <TEntity>; if (source == null) { source = this._dbSet.AsNoTracking().ToList <TEntity>(); CacheManager.Add(typeof(TEntity).FullName, (object)source); } entity = source.FirstOrDefault <TEntity>((Func <TEntity, bool>)(e => ((IEnumerable <object>)e.GetID()).Select <object, string>((Func <object, string>)(id => id.ToString())).SequenceEqual <string>(((IEnumerable <object>)keyValues).Select <object, string>((Func <object, string>)(id => id.ToString()))))); } else { entity = this._dbSet.Find(keyValues); } if ((object)entity is IAggregateRoot || (object)entity is IAutoReferenceLoad) { DbContext context = this._context as DbContext; if (context == null) { return(entity); } if (context.Entry <TEntity>(entity).State == EntityState.Detached) { this._dbSet.Attach(entity); } foreach (PropertyInfo property in entity.GetType().GetProperties()) { if (!((IEnumerable <ParameterInfo>)property.GetIndexParameters()).Any <ParameterInfo>()) { if ((object)entity is IAutoReferenceLoad && ((IEnumerable <Type>)property.PropertyType.GetInterfaces()).Any <Type>((Func <Type, bool>)(t => t.FullName == typeof(IObjectState).FullName))) { DbReferenceEntry dbReferenceEntry = context.Entry <TEntity>(entity).Reference(property.Name); if (!dbReferenceEntry.IsLoaded) { dbReferenceEntry.Load(); if (dbReferenceEntry.CurrentValue is ILogicalDeletable && ((ILogicalDeletable)dbReferenceEntry.CurrentValue).IsDeleted) { dbReferenceEntry.CurrentValue = (object)null; } } } else if ((object)entity is IAggregateRoot && ((IEnumerable <Type>)property.PropertyType.GetInterfaces()).Any <Type>((Func <Type, bool>)(t => t.Name == typeof(ICollection <>).Name)) && ((IEnumerable <Type>)property.PropertyType.GetGenericArguments()).Any <Type>((Func <Type, bool>)(type => ((IEnumerable <Type>)type.GetInterfaces()).Any <Type>((Func <Type, bool>)(t => t == typeof(IObjectState)))))) { DbCollectionEntry dbCollectionEntry = context.Entry <TEntity>(entity).Collection(property.Name); if (!dbCollectionEntry.IsLoaded) { if (((IEnumerable <Type>)property.PropertyType.GetGenericArguments()).Any <Type>((Func <Type, bool>)(type => ((IEnumerable <Type>)type.GetInterfaces()).Any <Type>((Func <Type, bool>)(t => t == typeof(ILogicalDeletable)))))) { IQueryable queryable = dbCollectionEntry.Query(); Type genericArgument = property.PropertyType.GetGenericArguments()[0]; ParameterExpression parameterExpression = Expression.Parameter(genericArgument, "en"); LambdaExpression lambdaExpression = Expression.Lambda((Expression)Expression.Equal((Expression)Expression.Property((Expression)parameterExpression, "IsDeleted"), (Expression)Expression.Constant((object)false, typeof(bool))), parameterExpression); MethodCallExpression methodCallExpression = Expression.Call(typeof(Queryable), "Where", new Type[1] { genericArgument }, queryable.Expression, (Expression)Expression.Quote((Expression)lambdaExpression)); queryable.Provider.CreateQuery((Expression)methodCallExpression).Load(); } else { dbCollectionEntry.Load(); } } } } } } return(entity); }