简体   繁体   中英

Error in configuration.cs after enable-migrations run

I've run into a bit of a brick wall after enabling migrations to allow additional properties to be added to ApplicationUser.

I'm getting the error:

Error 24 The type 'App.Models.ApplicationUser' cannot be used as type parameter 'TContext' in the generic type or method 'System.Data.Entity.Migrations.DbMigrationsConfiguration'. There is no implicit reference conversion from 'App.Models.ApplicationUser' to 'System.Data.Entity.DbContext'. C:\\projects\\App\\App\\Migrations\\Configuration.cs 8 27 App

Everything was working fine, although I did have to use the -force parameter to enable migrations. Like so:

enable-migrations -ContextTypeName App.Models.ApplicationUser -force

The configuration file that has been created is:

namespace App.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<App.Models.ApplicationUser>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

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

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

And the code in IndentityModels.cs:

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;


namespace App.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public string Domain { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }


        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            //userIdentity.AddClaim(new Claim("Domain", this.Domain));
            //userIdentity.AddClaim(new Claim("FirstName", this.FirstName));
            //userIdentity.AddClaim(new Claim("LastName", this.LastName));


            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

Any ideas would be greatly appreciated.

App.Models.ApplicationUser is not your context. It's ApplicationDbContext ...

So revert your changes and execute the proper command:

enable-migrations -ContextTypeName ApplicationDbContext -force

You have to select the data access(Db context) file(your file is : ApplicationDbContext ) . instead of selecting context file you are trying to do migration in Application User file. That's the error. If you are using package console for migrating the file, you must change the default project location as your Data Access file location.

在此处输入图片说明

Look into this picture, i have highlighted the area, that place you should change your default project as your Data context file(your file is : ApplicationDbContext ) and do migration.

happy coding..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM