예제 #1
0
 public void UpdateEntities <EntityType>(ObjectContext objCtx, string entitySetName, List <EntityType> newEntities) where EntityType : class
 {
     foreach (EntityType entity in newEntities)
     {
         objCtx.AttachTo(entitySetName, entity);
         objCtx.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
     }
     objCtx.SaveChanges();
 }
예제 #2
0
        public void Attach <TEntity>(TEntity entity) where TEntity : class
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            ObjectContext.AttachTo(GetEntityName <TEntity>(), entity);
        }
예제 #3
0
        /// <summary>
        /// </summary>
        public void Sync()
        {
            var entries = GetEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged).Where(entry => entry.TargetState == EntityState.Detached);

            // attach those target entity that are marked as detached

            foreach (var entry in entries)
                _context.AttachTo(entry.TargetEntityKey.EntitySetName, entry.TargetEntity);
        }
예제 #4
0
        /// <summary>
        /// Attaches an unattached entity to an context
        /// </summary>
        /// <param name="entity"></param>
        public void Attach(TEntity entity)
        {
            ObjectStateEntry entry = GetObjectStateEntry(entity);

            if (entry == null || entry.State == EntityState.Detached)
            {
                context.AttachTo(EntitySetName, entity);
            }
        }
 public override void UpdateUser(MembershipUser mUser)
 {
     try
     {
         IUser         user = (IUser)mUser;
         ObjectContext ctx  = GetCtx() as ObjectContext;
         ctx.AttachTo("UserSet", user);
         ctx.ObjectStateManager.GetObjectStateEntry(user).SetModified();
         ctx.SaveChanges();
     }
     catch (Exception ex)
     {
         throw new ProviderException("Failed to update user", ex);
     }
 }
예제 #6
0
        private static void InternalAttach(ObjectContext ctx, object entity, bool addToContext, string entitySetName, List <object> analyzedEntities)
        {
            var objectType   = ObjectContext.GetObjectType(entity.GetType());
            var associations = ctx.MetadataWorkspace.GetItems <AssociationType>(DataSpace.OSpace);
            var os           = ctx.MetadataWorkspace.GetItem <EntityType>(objectType.FullName, DataSpace.OSpace);
            var item         = (EntityType)ctx.MetadataWorkspace.GetEdmSpaceType(os);
            var value        = ((XElement)(item.KeyMembers[0].MetadataProperties.First(p => p.Name == "http://EF:InsertWhen").Value)).Value;
            var idType       = ((PrimitiveType)item.Members[item.KeyMembers[0].Name].TypeUsage.EdmType).ClrEquivalentType;
            var id           = entity.GetType().GetProperty(item.KeyMembers[0].Name).GetValue(entity, null);

            if (id.Equals(Convert.ChangeType(value, idType)))
            {
                if (addToContext)
                {
                    ctx.AddObject(entitySetName, entity);
                }
                foreach (var association in ctx.MetadataWorkspace.GetItems <AssociationType>(DataSpace.CSpace).Where(a => a.AssociationEndMembers[1].Name == objectType.Name))
                {
                    var propName               = association.ReferentialConstraints[0].ToProperties[0].Name;
                    var foreignKey             = entity.GetType().GetProperty(propName).GetValue(entity, null);
                    var principalEntity        = association.AssociationEndMembers[0].GetEntityType();
                    var principalIdInsertValue = ((XElement)(principalEntity.KeyMembers[0].MetadataProperties.First(p => p.Name == "http://EF:InsertWhen").Value)).Value;
                    var principalIdType        = ((PrimitiveType)principalEntity.Members[principalEntity.KeyMembers[0].Name].TypeUsage.EdmType).ClrEquivalentType;
                    var navPropName            = item.NavigationProperties.First(p => p.RelationshipType.FullName == association.FullName).Name;
                    var navObject              = entity.GetType().GetProperty(navPropName).GetValue(entity, null);
                    if (!foreignKey.Equals(Convert.ChangeType(principalIdInsertValue, idType)))
                    {
                        ctx.ObjectStateManager.GetObjectStateEntry(navObject).ChangeState(EntityState.Unchanged);
                    }
                }
            }
            else
            if (addToContext)
            {
                ctx.AttachTo(entitySetName, entity);
            }
        }
예제 #7
0
 private ObjectStateEntry AttachObjectStateEntry(EFEntityInfo entityInfo)
 {
     ObjectContext.AttachTo(entityInfo.EntitySetName, entityInfo.Entity);
     // Attach has lots of side effect - add has far fewer.
     return(GetObjectStateEntry(entityInfo));
 }
예제 #8
0
 /// <summary>
 /// 保存指定的实体变动
 /// </summary>
 /// <param name="entity">实体列表</param>
 /// <returns></returns>
 public int Save(TEntity entity)
 {
     _objectContext.AttachTo(EntitySetName, entity);
     _objectContext.SetAllModified(entity);
     return(_objectContext.SaveChanges());
 }