Configuration.cs file asp.net 5 mvc

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

Question

I am trying to create project on ASP.NET 5 MVC, so I want to add superuser to my db. In ASP.NET 4 MVC I had Configuration.cs file in my migration folder and I use this code to create superuser in 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" });
    }
  }
}

But I can't find the same file in my new project.

enter image description here

Can some1 help me? Thx.

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

Accepted Answer

I assume you use ASP.net 5 default template which uses ASP.NET MVC6 and Entity Framework 7. From your screenshot I can see you have a snapshot.cs file which was only introduced in EF7.

A seed configuration option hasn't been added to Entity Framework 7 yet (Beta 8) so as a workaround you'll have to create your own Seed Data class and run it at your app's start-up.

To determine if you should seed it in your database you can run Linq's Any() method to determine if the table is empty or use your own EF logic instead.

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

Then in your Startup.cs

Add toConfigureServices

services.AddTransient<DbSeedData>();

Add to Configure method the DBSeedData class as a parameter e.g.

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

At the end of this method add:

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

Popular Answer

I can't speak for MVC6, like firste was talking about, but in MVC5 it is possible to do the same thing.

In my case, i don't know if it created the configuration.cs file when it enabled migrations or at another point. However, even if there isn't, you should still be able to create one.

In my project, it created this base code:

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

This is from the MVC5 template/w auth from VS2013 Community, just FYI. This looks like the same code you use, but this works fine in mine and you should be able to copy either code over to your MVC5 project. I left the linq comments in for you, in case it helps anyone else.



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