基于声明的身份 – 在asp.net MVC5 EF6中使用流畅的api映射表?

前端之家收集整理的这篇文章主要介绍了基于声明的身份 – 在asp.net MVC5 EF6中使用流畅的api映射表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将个人资料/会员信息添加到我的MVC5应用程序中并添加配置映射.

我收到以下错误消息:

my.Models.IdentityUserLogin: : EntityType ‘IdentityUserLogin’ has no
key defined. Define the key for this EntityType.

my.Models.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.

public class ApplicationUser : IdentityUser
{
    public string City { get; set; }
    public string Discriminator { get; set; }

    public string Address { get; set; }     
}

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ApplicationUserConfiguration());           
    }
}

解决方法

调用base.OnModelCreating(modelBuilder)并没有解决我的问题.

在VS2013-Preview,VS2013-RC和VS2013-RTM中,Microsoft.AspNet.Identity.EntityFramework的行为似乎不同.我正在使用RTM版本.

从IdentityUser继承之后,我不得不重新创建模型中的所有其他主键,使其工作:

public class ApplicationUser : IdentityUser
{
    public string DisplayName { get; set; }
}


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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId,r.UserId });
    }

(见Configuring/Mapping Properties and Types with the Fluent API)

我猜AspNet.Identity.EntityFramework的工作正在进行中,这将被修复(?)

原文链接:https://www.f2er.com/aspnet/250466.html

猜你在找的asp.Net相关文章