【问题标题】:Relating tables to a Microsoft Identity User - has no key defined errors将表与 Microsoft 身份用户相关联 - 没有键定义错误
【发布时间】:2015-05-12 15:17:24
【问题描述】:

我遵循以下内容并使用ApplicationUser 而不是自己创建。 ApplicationUser 来自默认的 MVC 5 应用程序:

http://blogs.msdn.com/b/webdev/archive/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx

这是我的ApplicationUser 代码:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync( UserManager<ApplicationUser> manager )
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync( this, DefaultAuthenticationTypes.ApplicationCookie );
        // Add custom user claims here
        return userIdentity;
    }

    public virtual ICollection<Field> Fields { get; set; }
}

这是我的Field 代码:

public class Field
{
    [Key]
    public int FieldID { get; set; }

    // [ForeignKey( "Id" )] // Not sure if i needed this but neither worked
    public virtual ApplicationUser CreatedBy { get; set; }

    ...

但是当我运行PM&gt; Add-Migration AddedCreatedByToFields时出现以下错误:

One or more validation errors were detected during model generation:

FieldApplication.Domain.Concrete.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
FieldApplication.Domain.Concrete.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.

任何想法我做错了什么?

旁注

我将我的 ApplicationUser 代码移到了一个名为 Domains 的单独项目中。我包含了 Identity Nuget 包以获取其中的命名空间...

【问题讨论】:

  • 没有人回答是因为代码似乎正确吗?如果是这样,那将帮助我找到问题:)

标签: c# asp.net asp.net-mvc-5 asp.net-identity-2


【解决方案1】:

我最后放了这个:

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

进入我的 EFDbContext:

    protected override void OnModelCreating( DbModelBuilder modelBuilder )
    {
        // base.OnModelCreating( modelBuilder );
        Precision.ConfigureModelBuilder(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 } );
    }

然后我必须运行迁移,然后创建一整套新表...我最终保留了这些表...我删除了默认表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-03
    • 2011-01-07
    • 1970-01-01
    • 2016-10-18
    • 2018-03-26
    • 2014-02-01
    • 1970-01-01
    • 2017-12-17
    相关资源
    最近更新 更多