启用迁移后,我遇到了一些障碍,允许将其他属性添加到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();
}
}
}
非常感谢任何想法。
答案 0 :(得分:1)
App.Models.ApplicationUser
不是您的背景。它是ApplicationDbContext
...
因此,请恢复更改并执行正确的命令:
enable-migrations -ContextTypeName ApplicationDbContext -force
答案 1 :(得分:0)