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

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

问题描述



我收到以下错误消息:


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



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



IdentityUserLogins:EntityType:EntitySet'IdentityUserLogins'是基于不定义键的类型IdentityUserLogin的
。 >

IdentityUserRoles:EntityType:EntitySet'IdentityUserRoles'是基于
的类型'IdentityUserRole',没有定义键。




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

public string Address {get;组; }
}

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


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

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating (模型构建器);
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 am trying to add profile/Membership information into my MVC5 application and adding configuration mappings.

I get the following error message:

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

解决方案

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

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.

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

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

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

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

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