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

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


    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        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<object, object> 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("info@edaysoft.cn", 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<DbValidationError> _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<bd.models.ProductCategory> ProductCategories { get; set; }

        public System.Data.Entity.DbSet<bd.models.Product> Products { get; set; }
    }


public class ApplicationRoleManager : RoleManager<IdentityRole>
    {
        public ApplicationRoleManager(IRoleStore<IdentityRole, string> store)
            : base(store)
        { }

        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
        {
            IRoleStore<IdentityRole,string> store = new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>());
            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<DbValidationError> _validationErrors)
        {
            _validationErrors = new List<DbValidationError>();
            return true;
        }
    }

internal sealed class Configuration : DbMigrationsConfiguration<bd.web.Models.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(bd.web.Models.ApplicationDbContext context)
        {
            InitAdmin(context);
        }
        private static void InitAdmin(ApplicationDbContext context)
        {
            string email = "yunying.liu@Live.com";
            ApplicationUserManager um = null;
            ApplicationRoleManager rm = null;
            if (HttpContext.Current != null && HttpContext.Current.GetOwinContext() != null)
            {
                um = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
                rm = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationRoleManager>();
            }
            if (um == null)
                um = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
            if (rm == null)
                rm = new ApplicationRoleManager(new RoleStore<IdentityRole>(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");
        }
    }






版权声明:本文为zhaoyaoxing原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。