zoukankan      html  css  js  c++  java
  • Code First 数据库初始化

    Global.asax

            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
    
                Database.SetInitializer<StarObjectContext>(new StarInitializer());
            }

    StarInitializer

    View Code
    using System.Data.Entity;
    using Star.Core.Models;
    
    namespace Star.Core.Data
    {
        public class StarInitializer : DropCreateDatabaseIfModelChanges<StarObjectContext>  
        {
            protected override void Seed(StarObjectContext context)  
            {  
                //base.Seed(context);  
                Account admin  = new Account{
                    Email = "admin@**.cn",
                    Password = "...",
                    Permission = "admin",
                    Stutas = AccountStutas.Enable,
                    LastLoginTime = DateTime.Now,
                    CreateTime = DateTime.Now
                };
                context.Accounts.Add(admin);
    
                List<Category> categorys = new List<Category> {
                    new Category{
                        Name = "category1",
                        ShowOnHomepage = true
                    },
                    new Category{
                        Name = "category2",
                        ShowOnHomepage = true
                    }
                };
    
                categorys.ForEach(d => context.Categorys.Add(d));
                
                context.SaveChanges();
            }  
        }
    }

    StarObjectContext

        public class StarObjectContext : DbContext//, IDbContext
        {
            public StarObjectContext(string nameOrConnectionString)
                : base(nameOrConnectionString)
            {
                //((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
            }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Account>().ToTable("Account");
                modelBuilder.Entity<Category>().ToTable("Category");
                base.OnModelCreating(modelBuilder);
            }
    
            public DbSet<Account> Accounts { get; set; }
            public DbSet<Category> Categorys { get; set; }
        }
    

    参考资料:http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

  • 相关阅读:
    react native配置ip真机测试
    APP Store上架QA&注意事项
    iOS 开发】解决使用 CocoaPods 执行 pod install 时出现
    iphoneX适配!!!
    better-scroll和swiper使用中的坑
    js知识巩固
    vue的学习(常用功能)
    vue学习生命周期(created和mounted区别)
    jq常用功能操作
    移动端中遇到的坑(bug)!!!
  • 原文地址:https://www.cnblogs.com/season2009/p/2824587.html
Copyright © 2011-2022 走看看