Configuration.cs file asp.net 5 mvc

asp.net-mvc-5 c# entity-framework-6

Question

I want to add superuser to my database because I'm trying to develop an ASP.NET 5 MVC app. I had a Configuration.cs file for ASP.NET 4 MVC in my migration folder, and I used the following code to create the superuser in the Seed method:

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" });
    }
  }
}

The same file, however, is not included in my new project.

enter image description here

Can somebody assist me? Thx.

1
1
11/2/2015 12:37:42 PM

Accepted Answer

I'm assuming you use the ASP.NET 5 default template, which makes use of Entity Framework 7 and ASP.NET MVC6. I note from your screenshot that you are using a snapshot.cs file, which was a brand-new feature in EF7.

Entity Framework 7 (Beta 8) does not currently have a seed configuration option, therefore as a workaround you will need to develop your own Seed Data class and run it when your app first launches.

You can use your own EF logic or Linq's Any() method to check whether the table is empty before deciding whether to seed it in your database.

e.g.

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" }
                 )
            }
        }
    }

Next, in yourStartup.cs

Add toConfigureServices

services.AddTransient<DbSeedData>();

Add toConfigure the DBSeedData class as a parameter, for example.

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

Add the following after this method:

seeder.EnsureSeedData();
4
11/2/2015 2:39:53 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow