Esempio n. 1
0
        public static async Task <EntityEntry> RefreshAsync(this EntityEntry tracking, RefreshConflict refreshMode)
        {
            switch (refreshMode)
            {
            case RefreshConflict.StoreWins:
                {
                    await tracking.ReloadAsync();

                    break;
                }

            case RefreshConflict.ClientWins:
                {
                    PropertyValues databaseValues = await tracking.GetDatabaseValuesAsync();

                    if (databaseValues == null)
                    {
                        tracking.State = EntityState.Detached;
                    }
                    else
                    {
                        tracking.OriginalValues.SetValues(databaseValues);
                    }
                    break;
                }

            case RefreshConflict.MergeClientAndStore:
                {
                    PropertyValues databaseValues = await tracking.GetDatabaseValuesAsync();

                    if (databaseValues == null)
                    {
                        tracking.State = EntityState.Detached;
                    }
                    else
                    {
                        PropertyValues originalValues = tracking.OriginalValues.Clone();
#if !EF
                        originalValues.SetValues(tracking.OriginalValues);
#endif
                        tracking.OriginalValues.SetValues(databaseValues);
#if EF
                        databaseValues.PropertyNames
                        .Where(property => !object.Equals(originalValues[property], databaseValues[property]))
                        .ForEach(property => tracking.Property(property).IsModified = false);
#else
                        databaseValues.Properties
                        .Where(property => !object.Equals(originalValues[property.Name], databaseValues[property.Name]))
                        .ForEach(property => tracking.Property(property.Name).IsModified = false);
#endif
                    }
                    break;
                }
            }
            return(tracking);
        }
        /// <summary>
        /// auto update create at, by, update at, mod by
        /// </summary>
        /// <param name="user">user.name</param>
        /// <returns></returns>
        //public int SaveChanges(/*string user*/)
        //{
        //    //RenewMacroColumn(user);
        //    return SaveChanges();
        //}
        /// <summary>
        /// base save changes
        /// </summary>
        /// <returns></returns>
        //public override int SaveChanges()
        //{
        //    try
        //    {
        //        return base.SaveChanges();
        //    }
        //    catch (DbEntityValidationException ex)
        //    {
        //        Exception raise = ex;
        //        foreach (var validationErrors in ex.EntityValidationErrors)
        //        {
        //            foreach (var validationError in validationErrors.ValidationErrors)
        //            {
        //                string message = string.Format("{0}:{1}",
        //                    validationErrors.Entry.Entity.ToString(),
        //                    validationError.ErrorMessage);
        //                // raise a new exception nesting
        //                // the current instance as InnerException
        //                raise = new InvalidOperationException(message, raise);
        //            }
        //        }
        //        return -1;
        //    }
        //}


        //protected void RenewMacroColumn(string user)
        //{
        //    foreach (var dbEntry in this.ChangeTracker.Entries())
        //    {
        //        switch (dbEntry.State)
        //        {

        //            case EntityState.Added:
        //                CreateWithValues(dbEntry, "GID", System.Guid.NewGuid());
        //                CreateWithValues(dbEntry, "CreatedAt", System.DateTime.Now);

        //                CreateWithValues(dbEntry, "ModBy", user);
        //                CreateWithValues(dbEntry, "UpdatedAt", System.DateTime.Now);
        //                break;

        //            case EntityState.Modified:

        //                CreateWithValues(dbEntry, "ModBy", user);
        //                CreateWithValues(dbEntry, "UpdatedAt", System.DateTime.Now);
        //                break;
        //        }
        //    }
        //}

        private void CreateWithValues(System.Data.Entity.Infrastructure.DbEntityEntry dbEntry, string propertyName, object value)
        {
            if (dbEntry.CurrentValues.PropertyNames.Any(x => x == propertyName))
            {
                dbEntry.Property(propertyName).CurrentValue = value;
            }
        }
Esempio n. 3
0
            public void EntityEntity_can_be_obtained_from_non_generic_DbPropertyEntry_back_reference()
            {
                var entityEntry = new DbEntityEntry <FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object);

                var backEntry = entityEntry.Property("ValueTypeProp").EntityEntry;

                Assert.Same(entityEntry.Entity, backEntry.Entity);
            }
Esempio n. 4
0
            public void Parent_PropertyEntity_returns_null_for_non_nested_non_generic_DbPropertyEntry_back_reference()
            {
                var entityEntry = new DbEntityEntry <FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object);

                var backEntry = entityEntry.Property("ValueTypeProp").ParentProperty;

                Assert.Null(backEntry);
            }
Esempio n. 5
0
        public void Edit(TEntity model, string[] propertys)

        {
            if (model == null)
            {
                throw new Exception(" 实体不能为空");
            }
            if (propertys.Any() == false)
            {
                throw new Exception("要修改的属性至少有一个");
            }
            // 2.0 将 model 追加到EF容器
            System.Data.Entity.Infrastructure.DbEntityEntry entry = db.Entry(model);
            entry.State = System.Data.EntityState.Unchanged;
            foreach (var item in propertys)
            {
                entry.Property(item).IsModified = true;
            }
            //3.0 关闭EF对于实体的合法性验证参数
            db.Configuration.ValidateOnSaveEnabled = false;
        }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model">没有被ef容器管理的</param>
        /// <param name="propertyNames"></param>
        public void Edit(TEntity model, string[] propertyNames)
        {
            //1.0 合法性验证
            if (model == null)
            {
                throw new Exception("Edit中的参数model不能为null");
            }

            if (propertyNames == null || propertyNames.Any() == false)
            {
                throw new Exception("Edit中的参数propertyNames至少要有一个属性");
            }

            //2.0 将model追加到EF容器中
            System.Data.Entity.Infrastructure.DbEntityEntry entry = db.Entry(model);
            entry.State = System.Data.EntityState.Unchanged;
            foreach (var item in propertyNames)
            {
                entry.Property(item).IsModified = true;
            }

            //3.0 关闭EF的实体属性合法性验证
            db.Configuration.ValidateOnSaveEnabled = false;
        }