2015-11-02 76 views
1

我想在ASP.NET 5 MVC上創建項目,所以我想添加超級用戶到我的數據庫。在ASP.NET MVC 4我Configuration.cs文件在我的移民文件夾,我用這個代碼在種方法來創建超級用戶:Configuration.cs文件asp.net 5 mvc

namespace WebApp.Migrations 
{ 
using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 
using System; 
using System.Data.Entity; 
using System.Data.Entity.Migrations; 
using System.Linq; 
using WebApp.Models; 

internal sealed class Configuration : DbMigrationsConfiguration<WebApp.Models.ApplicationDbContext> 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = false; 
     // AutomaticMigrationsEnabled = true; 
     // AutomaticMigrationDataLossAllowed = true; 
    } 

    protected override void Seed(WebApp.Models.ApplicationDbContext context) 
    { 
     // This method will be called after migrating to the latest version. 

     var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 

     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext())); 

     var user = new ApplicationUser() 
     { 
      UserName = "***", 
      Email = "***", 
      Guid="0", 
      EmailConfirmed = true 
     }; 

     manager.Create(user, "***"); 

     if (roleManager.Roles.Count() == 0) 
     { 
      roleManager.Create(new IdentityRole { Name = "admin" }); 
     } 

     var adminUser = manager.FindByName("***"); 

     manager.AddToRoles(adminUser.Id, new string[] { "admin" }); 
    } 
    } 
} 

但我無法找到我的新項目相同的文件。

enter image description here

some1能幫我嗎?謝謝。

回答

4

我假設你使用ASP.net 5默認模板,它使用ASP.NET MVC6和實體框架7.從你的截圖我可以看到你有一個snapshot.cs文件,它只在EF7中引入。

種子配置選項尚未添加到Entity Framework 7(Beta 8)中,因此您必須創建自己的Seed Data類並在應用程序啓動時運行它。

要確定是否應該在數據庫中播種它,可以運行Linq的Any()方法來確定該表是否爲空或者使用您自己的EF邏輯。

例如

public class DbSeedData 
{ 
    private MyDb _context; 

    public DbSeedData(MyDb db) 
    { 
     _context = db; 
    } 


    public void EnsureSeedData() 
    { 
     if (!_context.Entities.Any()) 
     { 
      { 
       _context.Entities.Add(
        new Entity{EntityName="blabla" }, 
        new Entity{EntityName="blabla2" } 
       ) 
      } 
     } 
    } 
Startup.cs

然後添加到ConfigureServices

services.AddTransient<DbSeedData>(); 

添加到Configure方法DBSeedData類作爲參數例如

public void Configure(IApplicationBuilder app, DbSeedData seeder, IHostingEnvironment env, ILoggerFactory loggerFactory) 

在這個方法的末尾添加:

seeder.EnsureSeedData(); 
1

我不能MVC6說話,像firste在說什麼,但在MVC5也可以做同樣的事情。

在我的情況下,我不知道它是否在創建configuration.cs文件時啓用遷移或在其他點。但是,即使沒有,你仍然應該能夠創建一個。

在我的項目,創造了這個基本代碼:

using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 
using WebSiteName.Models; 
using System; 
using System.Configuration; 
using System.Data.Entity; 
using System.Data.Entity.Migrations; 
using System.Linq; 

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

     protected override void Seed(WebSiteName.Models.ApplicationDbContext context) 
     { 
      // This method will be called after migrating to the latest version. 

      // Linq Method 
      /* 
      * var passwordHash = new PasswordHasher(); 
      * string password = passwordHash.HashPassword("[email protected]"); 
      * context.Users.AddOrUpdate(u => u.UserName, 
      * new ApplicationUser 
      * { 
      *  UserName = "[email protected]", 
      *  PasswordHash=password, 
      *  PhoneNumber = "08869879" 
      * 
      * }); 
      */ 
     } 
    } 
} 

這是從MVC5模板/ W AUTH從VS2013社區,僅供參考。這看起來像你使用的相同的代碼,但這在我的工作正常,你應該能夠將任何代碼複製到您的MVC5項目。我留下了linq的評論,以防其他人幫忙。