【问题标题】:Configuration.cs file asp.net 5 mvcConfiguration.cs 文件 asp.net 5 mvc
【发布时间】: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


    【解决方案1】:

    我假设您使用 ASP.net 5 默认模板,该模板使用 ASP.NET MVC6 和 Entity Framework 7。从您的屏幕截图中,我可以看到您有一个仅在 EF7 中引入的 snapshot.cs 文件。

    实体框架 7(Beta 8)尚未添加种子配置选项,因此作为一种解决方法,您必须创建自己的种子数据类并在应用启动时运行它。

    要确定是否应该在数据库中播种,您可以运行 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>();
    

    将 DBSeedData 类作为参数添加到 Configure 方法,例如

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

    在这个方法的最后加上:

    seeder.EnsureSeedData();
    

    【讨论】:

      【解决方案2】:

      我不能像 firste 所说的那样代表 MVC6,但在 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("Password@123");
                   * context.Users.AddOrUpdate(u => u.UserName,
                   *   new ApplicationUser
                   *   {
                   *       UserName = "Steve@Steve.com",
                   *       PasswordHash=password,
                   *       PhoneNumber = "08869879"
                   * 
                   *   });
                   */
              }
          }
      }
      

      这是来自 VS2013 社区的 MVC5 模板/w auth,仅供参考。这看起来与您使用的代码相同,但在我的情况下可以正常工作,您应该能够将任一代码复制到您的 MVC5 项目中。我为你留下了 linq cmets,以防它帮助其他人。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-07
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多