我一直在試圖做到這一點,我無法找到解決方案。任何幫助表示讚賞,鏈接到一個例子或簡單的教程,我只是想與MVC熟悉。我很抱歉,如果這已被回答,但我無法找到一個工作解決方案。 我想使用Identity ASPNETUSER表中存儲的電子郵件字段作爲另一個表中的外部主鍵。這是我正在玩的一些代碼,看看我是否得到它的工作,但得到這個錯誤: 「運行選定的代碼生成器時出錯:'無法檢索'ZooMenagerie.Models.UserDetails'的元數據。對象設置'ApplicationUsers'和'Users'都可以包含'ZooMenagerie.Models.ApplicationUser'類型的實例'「 這個錯誤出現在我試圖支持UserDetails Model,數據上下文我選擇的類叫做ApplicationDbContext(ZooMenagerie.Models)。 N.B - 我有兩個Context類,一個是MVC自帶的'ApplicationDbContext'和'ZooMenagerieContext'。我指出ApplicationDbContext指向相同的數據庫,如果這是我做錯了,請讓我知道。使用ASPNETUSER表電子郵件字段作爲另一個表中的主鍵,主外鍵
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; }
}
}