使用 ASPNETUSER Table 电子邮件字段作为另一个 table 中的主键,主外键

Use ASPNETUSER Table Email Field as a primary key in another table, primary foreign key

我一直在尝试这样做,但找不到解决方案。感谢任何帮助,链接到示例或简单教程,我只是想熟悉 MVC。如果这个问题已经得到解答,我深表歉意,但我找不到可行的解决方案。 我想使用存储在身份 ASPNETUSER table 中的电子邮件字段作为另一个 table 中的外部主键。这是我正在玩的一些代码,看看我是否让它工作,但出现了这个错误: "There was an error running the selected code generator:'Unable to retrieve metadata for 'ZooMenagerie.Models.UserDetails'. Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'ZooMenagerie.Models.ApplicationUser'.'" 在我尝试构建我的 UserDetails 模型后出现此错误,我 select 的数据上下文 class 称为 ApplicationDbContext(ZooMenagerie.Models)。 N.B - 我有两个上下文 class,一个是 MVC 附带的 'ApplicationDbContext' 和 'ZooMenagerieContext'。我已将 ApplicationDbContext 指向同一个数据库,如果这是我做错了什么,也请告诉我。

IdentityModels.cs

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace ZooMenagerie.Models
{
// You can add profile data for the user by adding more properties to your          ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
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 UserDetails UserDetails { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("name=ZooMenagerieContext", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<ZooMenagerie.Models.UserDetails> UserDetails { get; set; }

    public System.Data.Entity.DbSet<ZooMenagerie.Models.ApplicationUser> ApplicationUsers { get; set; }
}
}    

UserDetails.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace ZooMenagerie.Models
{
public class UserDetails
{
    [Key, ForeignKey("Id")]
    public string knumber { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

}

我做了一个变通方法,我重命名为:

  public virtual ApplicationUser ApplicationUser { get; set; }

  public virtual ApplicationUser User { get; set; }

重构代码后,我构建了我的代码,删除了

public System.Data.Entity.DbSet<ZooMenagerie.Models.ApplicationUser> ApplicationUsers { get; set; }

转到我的控制器并将 ApplicationUser 的任何使用更改为用户,因此例如在我的索引中 class 看起来像这样:

public ActionResult Index()
    {
        var userDetails = db.UserDetails.Include(u => u.User);
        return View(userDetails.ToList());
    }

我的控制器都使用 User 而不是 applicationuser。