Example #1
0
 public static void Initialize(IServiceProvider serviceProvider)
 {
     using (var context = new LifeDbContext(serviceProvider.GetRequiredService <DbContextOptions <LifeDbContext> >()))
     {
         if (context.Users.Any())
         {
             return;   // 已经初始化过数据,直接返回
         }
         Guid teamId = Guid.NewGuid();
         context.Teams.Add(new Team
         {
             Id         = teamId,
             TeamName   = "603",
             TotalMoney = 100,
         });
         List <User> users = new List <User>
         {
             new User
             {
                 Id       = new Guid(),
                 UserName = "******",
                 PassWord = "******",
                 NickName = "疯疯过"
             },
             new User
             {
                 Id       = new Guid(),
                 UserName = "******",
                 PassWord = "******",
                 NickName = "翁超群"
             }
         };
         context.Users.AddRange(users);
         context.SaveChanges();
     }
 }
 /// <summary>
 /// 通过构造函数注入得到数据上下文对象实例
 /// </summary>
 /// <param name="dbContext"></param>
 public EFRepositoryBase(LifeDbContext dbContext)
 {
     _dbContext = dbContext;
 }
        public static void EnsureSendData(this LifeDbContext context)
        {
            if (context.Teams.Any() || context.Users.Any())
            {
                return;   // 已经初始化过数据,直接返回
            }
            Guid teamId = Guid.NewGuid();

            context.Teams.Add(new Team
            {
                Id         = teamId,
                TeamName   = "603",
                TotalMoney = 100,
            });
            List <User> users = new List <User>
            {
                new User
                {
                    Id       = new Guid(),
                    UserName = "******",
                    PassWord = EncryptionMD5.Get32MD5One("123456"),
                    NickName = "柳一雄",
                    TeamId   = teamId
                },
                new User
                {
                    Id       = new Guid(),
                    UserName = "******",
                    PassWord = EncryptionMD5.Get32MD5One("123456"),
                    NickName = "胡秋彦",
                    TeamId   = teamId
                },
                new User
                {
                    Id       = new Guid(),
                    UserName = "******",
                    PassWord = EncryptionMD5.Get32MD5One("654321"),
                    NickName = "陈敏航",
                    TeamId   = teamId
                },
                new User
                {
                    Id       = new Guid(),
                    UserName = "******",
                    PassWord = EncryptionMD5.Get32MD5One("123456"),
                    NickName = "邹立杰",
                    TeamId   = teamId
                },
                new User
                {
                    Id       = new Guid(),
                    UserName = "******",
                    PassWord = EncryptionMD5.Get32MD5One("ffg123"),
                    NickName = "翁超群",
                    TeamId   = teamId
                }
            };

            context.Users.AddRange(users);
            context.SaveChanges();
        }