在 ASP.NET MVC 5 中使用 Entity Framework 代码优先添加涉及引用 ApplicationUser 的实体的迁移问题
Issue adding migration involving entity that reference ApplicationUser in ASP.NET MVC 5 with Entity Framework code-first
ASP.NET MVC 5,当涉及引用 ApplicationUser
实体的实体时,我在添加迁移时遇到了一些问题。我创建项目时使用的是个人账号认证
public class Blog
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[ForeignKey("ApplicationUser")]
public string ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
[Required]
[StringLength(500, ErrorMessage = "The {0} must be at least {2} characters long.")]
public string Content { get; set; }
}
public class ApplicationUser : IdentityUser
{
public bool IsSoftDeleted { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
我的 DBContexts 是
public class ApplicationUserDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationUserDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationUserDbContext Create()
{
return new ApplicationUserDbContext();
}
}
public class BloggingServiceDbContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public BloggingServiceDbContext() : base("DefaultConnection")
{
}
}
我在包管理控制台上 运行 的脚本是:
Add-Migration InitialBlogMigration -ConfigurationTypeName BloggerConfiguration
我得到的错误是:
BloggingService.Web.Context.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
BloggingService.Web.Context.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
注意:在实施 Blog
之前,运行ning add-migration
没有问题,并且数据库不包含错误消息中提到的任何实体。
我 运行 找不到可能是什么问题的线索。你能帮我找出问题所在吗?
解决方法似乎很简单,我需要将引用 ApplicationUser 的所有实体都放置到 ApplicationUserDbContext 中。但我仍然不知道为什么会这样,如果有人能为我澄清一下就好了。
ASP.NET MVC 5,当涉及引用 ApplicationUser
实体的实体时,我在添加迁移时遇到了一些问题。我创建项目时使用的是个人账号认证
public class Blog
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[ForeignKey("ApplicationUser")]
public string ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
[Required]
[StringLength(500, ErrorMessage = "The {0} must be at least {2} characters long.")]
public string Content { get; set; }
}
public class ApplicationUser : IdentityUser
{
public bool IsSoftDeleted { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
我的 DBContexts 是
public class ApplicationUserDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationUserDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationUserDbContext Create()
{
return new ApplicationUserDbContext();
}
}
public class BloggingServiceDbContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public BloggingServiceDbContext() : base("DefaultConnection")
{
}
}
我在包管理控制台上 运行 的脚本是:
Add-Migration InitialBlogMigration -ConfigurationTypeName BloggerConfiguration
我得到的错误是:
BloggingService.Web.Context.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
BloggingService.Web.Context.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
注意:在实施 Blog
之前,运行ning add-migration
没有问题,并且数据库不包含错误消息中提到的任何实体。
我 运行 找不到可能是什么问题的线索。你能帮我找出问题所在吗?
解决方法似乎很简单,我需要将引用 ApplicationUser 的所有实体都放置到 ApplicationUserDbContext 中。但我仍然不知道为什么会这样,如果有人能为我澄清一下就好了。