加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:30:35 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试将个人资料/会员信息添加到我的MVC5应用程序中并添加配置映射. 我收到以下错误消息: my.Models.IdentityUserLogin: : EntityType ‘IdentityUserLogin’ has no key defined. Define the key for this EntityType. my.Models.IdentityUserRole: :
我正在尝试将个人资料/会员信息添加到我的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的工作正在进行中,这将被修复(?)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读