繁体   English   中英

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

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

我一直在尝试这样做,却无法找到解决方案。 感谢任何帮助,可以链接到示例或简单教程,我只是想熟悉MVC。 抱歉,如果无法解决,我将无法找到有效的解决方案。 我想将存储在Identity ASPNETUSER表中的Email字段用作另一个表中的外键。 这是我正在尝试的一些代码,以查看是否能正常工作,但收到以下错误:“运行所选代码生成器时出错:'无法检索'ZooMenagerie.Models.UserDetails'的元数据。不支持每种类型的集合。对象集'ApplicationUsers'和'Users'都可以包含类型为'ZooMenagerie.Models.ApplicationUser'的实例。'“在尝试架设UserDetails模型(即数据上下文类)后,将出现此错误我选择称为ApplicationDbContext(ZooMenagerie.Models)。 注意:我有两个Context类,一个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的任何使用更改为User,因此例如在我的索引类中如下所示:

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

我的控制器全部使用了User而不是applicationuser。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM