在整体的DbContext上针对所有的通用列进行赋值

在整体的DbContext上针对所有的通用列进行赋值(CreateTime、CreatorName等)


    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        #region ValidateEntity
        private static DateTime _standard_time = new DateTime(1986, 1, 21);
        protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary items)
        {
            BaseTopModel data = null;
            if (entityEntry != null && entityEntry.Entity is BaseTopModel)
            {
                data = entityEntry.Entity as BaseTopModel;
                if (data != null)
                {
                    if (data.CreateTime < _standard_time)
                    {
                        data.CreateTime = DateTime.Now;
                    }
                    if (data.LastUpdateTime < _standard_time)
                    {
                        data.LastUpdateTime = DateTime.Now;
                    }

                    if ((string.IsNullOrWhiteSpace(data.CreatorName) && (data.CreatorName == null))
                        || (string.IsNullOrWhiteSpace(data.LastUpdateName) && (data.LastUpdateName == null))
                        || (entityEntry.State == EntityState.Modified))
                    {
                        string currentUid = null;
                        try
                        {
                            currentUid = HttpContext.Current.User.Identity.GetUserId();
                            if (string.IsNullOrWhiteSpace(currentUid))
                            {
                                throw new Exception("获取用户信息失败");
                            }
                        }
                        catch (Exception ex)
                        {
                            var yunying = Users.FirstOrDefaultAsync(o => o.Email.Equals("[email protected]", StringComparison.OrdinalIgnoreCase));
                            if (yunying.Result == null)
                            {
                                throw ex;
                            }
                            currentUid = yunying.Result.Id;
                        }
                        if (string.IsNullOrWhiteSpace(data.CreatorName) && (data.CreatorName == null))
                        {
                            data.CreatorName = currentUid;
                        }
                        if (string.IsNullOrWhiteSpace(data.LastUpdateName) && (data.LastUpdateName == null))
                        {
                            data.LastUpdateName = currentUid;
                        }
                        if (entityEntry.State == EntityState.Modified)
                        {
                            data.LastUpdateName = currentUid;
                            data.LastUpdateTime = DateTime.Now;
                        }
                    }

                    ICollection _validationErrors = null;
                    bool isValid = data.CheckValidition(out _validationErrors);
                    if (!isValid)
                    {
                        DbEntityValidationResult result = new DbEntityValidationResult(entityEntry, _validationErrors);
                        return result;
                    }
                }
            }

            return base.ValidateEntity(entityEntry, items);
        }

        #endregion

        public System.Data.Entity.DbSet ProductCategories { get; set; }

        public System.Data.Entity.DbSet Products { get; set; }
    }


public class ApplicationRoleManager : RoleManager
    {
        public ApplicationRoleManager(IRoleStore store)
            : base(store)
        { }

        public static ApplicationRoleManager Create(IdentityFactoryOptions options, IOwinContext context)
        {
            IRoleStore store = new RoleStore(context.Get());
            return new ApplicationRoleManager(store);
        }


    }
    public class BaseTopModel
    {
        public DateTime CreateTime { get; set; }
        public DateTime LastUpdateTime { get; set; }
        public string CreatorName { get; set; }
        public string LastUpdateName { get; set; }
        public bool Deleted { get; set; }
        public virtual bool CheckValidition(out ICollection _validationErrors)
        {
            _validationErrors = new List();
            return true;
        }
    }

internal sealed class Configuration : DbMigrationsConfiguration
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(bd.web.Models.ApplicationDbContext context)
        {
            InitAdmin(context);
        }
        private static void InitAdmin(ApplicationDbContext context)
        {
            string email = "[email protected]";
            ApplicationUserManager um = null;
            ApplicationRoleManager rm = null;
            if (HttpContext.Current != null && HttpContext.Current.GetOwinContext() != null)
            {
                um = HttpContext.Current.GetOwinContext().GetUserManager();
                rm = HttpContext.Current.GetOwinContext().GetUserManager();
            }
            if (um == null)
                um = new ApplicationUserManager(new UserStore(context));
            if (rm == null)
                rm = new ApplicationRoleManager(new RoleStore(context));
            ApplicationUser yunying = um.FindByName(email);
            IdentityRole role = rm.FindByName("super");
            if (yunying == null)
            {

                yunying = new ApplicationUser { UserName = email, Email = email };
                var result = um.Create(yunying, "Asdf`1234");
                if (!result.Succeeded)
                {
                    throw new Exception("初始化数据失败。");
                }
            }
            if (role == null)
                rm.Create(new IdentityRole("super"));
            if (!um.IsInRole(yunying.Id, "super"))
                um.AddToRole(yunying.Id, "super");
        }
    }





你可能感兴趣的:(ASP.NET,C#.NET)