【发布时间】:2015-11-02 12:37:42
【问题描述】:
我正在尝试在 ASP.NET 5 MVC 上创建项目,所以我想将超级用户添加到我的数据库中。在 ASP.NET 4 MVC 中,我的迁移文件夹中有 Configuration.cs 文件,我使用此代码在 Seed 方法中创建超级用户:
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" });
}
}
}
但我在新项目中找不到相同的文件。
有人可以帮我吗?谢谢。
【问题讨论】:
标签: c# asp.net-mvc-5 entity-framework-6