简体   繁体   中英

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

I have been trying to do this and am unable to find a solution to this. Any help is appreciated, links to an example or simple tutorial, I am just trying to get familar with MVC. I apologise if this has already been answered but I am unable to find a working solution. I want to use the Email field that is stored in the Identity ASPNETUSER table as a foreign primary key in an another table. This is some code I was playing around with to see if I got it to work, but got this error: "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'.'" This error appears after I try to scaffold my UserDetails Model, the data context class that I select is called ApplicationDbContext(ZooMenagerie.Models). NB - I have two Context class, one that MVC comes with 'ApplicationDbContext' and 'ZooMenagerieContext'. I have point the ApplicationDbContext to point to the same database, if this is something I have done wrong please let me know as well.

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

}

I did a workaround where I renamed:

  public virtual ApplicationUser ApplicationUser { get; set; }

to

  public virtual ApplicationUser User { get; set; }

After then refactoring the code I scaffolded my code, removed

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

went to my controller and changed any use of ApplicationUser to be User, so for eg in my index class looked like this:

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

my controllers all used User instead of applicationuser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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