在 asp.net MVC5 EF6 中使用流利的 api 映射表? [英] Map tables using fluent api in asp.net MVC5 EF6?

查看:14
本文介绍了在 asp.net MVC5 EF6 中使用流利的 api 映射表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将个人资料/会员信息添加到我的 MVC5 应用程序中并添加配置映射.

I am trying to add profile/Membership information into my MVC5 application and adding configuration mappings.

我收到以下错误消息:

my.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' 没有键定义.定义此 EntityType 的键.

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

my.Models.IdentityUserRole: : EntityType 'IdentityUserRole' 没有密钥定义.定义此 EntityType 的键.

my.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.

IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' 是基于没有定义键的类型IdentityUserLogin".

IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' 是基于在没有定义键的类型IdentityUserRole"上.

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) 并没有解决我的问题.

Calling base.OnModelCreating(modelBuilder) did not solve the issue for me.

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

The behavior of Microsoft.AspNet.Identity.EntityFramework seems to be different in VS2013-Preview, VS2013-RC, and VS2013-RTM. I'm using the RTM version.

从 IdentityUser 继承后,我必须重新创建模型中的所有其他主键才能使其正常工作:

After inheriting from IdentityUser I had to recreate all other primary keys in the model to make it work:

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

(请参阅使用 Fluent API 配置/映射属性和类型)

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

I guess work on AspNet.Identity.EntityFramework is ongoing and this will be fixed (?)

这篇关于在 asp.net MVC5 EF6 中使用流利的 api 映射表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆