启用enable-migrations后,configuration.cs出错

时间:2015-10-27 10:40:00

标签: c# asp.net asp.net-mvc entity-framework

启用迁移后,我遇到了一些障碍,允许将其他属性添加到ApplicationUser。

我收到了错误:

  

错误24“App.Models.ApplicationUser”类型不能用作类型   泛型类型或方法中的参数'TContext'   'System.Data.Entity.Migrations.DbMigrationsConfiguration'。   没有隐式引用转换   'App.Models.ApplicationUser'来   'System.Data.Entity.DbContext'。 C:\ projects \ App \ App \ Migrations \ Configuration.cs 8 27 App

一切都运行正常,但我确实必须使用-force参数来启用迁移。像这样:

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

已创建的配置文件是:

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

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

非常感谢任何想法。

2 个答案:

答案 0 :(得分:1)

App.Models.ApplicationUser不是您的背景。它是ApplicationDbContext ...

因此,请恢复更改并执行正确的命令:

enable-migrations -ContextTypeName ApplicationDbContext -force

答案 1 :(得分:0)

您必须选择数据访问(Db上下文)文件(您的文件是: ApplicationDbContext )。您正尝试在应用程序用户文件中执行迁移,而不是选择上下文文件。那是错误。如果使用程序包控制台迁移文件,则必须将默认项目位置更改为数据访问文件位置。

enter image description here

查看此图片,我突出显示了该区域,您应该将默认项目更改为您的数据上下文文件(您的文件是: ApplicationDbContext )并进行迁移。

快乐的编码..!