모든 일반 열에 대한 전체 DbContext 값 지정

7153 단어 ASP.NETC#.NET
모든 일반 열에 대한 전체 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");
        }
    }

좋은 웹페이지 즐겨찾기