【发布时间】:2016-06-07 16:41:33
【问题描述】:
我一直在尝试这样做,但无法找到解决方案。任何帮助表示赞赏,链接到示例或简单教程,我只是想熟悉 MVC。如果这个问题已经得到解答,我深表歉意,但我无法找到可行的解决方案。 我想使用存储在 Identity ASPNETUSER 表中的电子邮件字段作为另一个表中的外主键。这是我正在玩的一些代码,看看我是否让它工作,但得到了这个错误: “运行所选代码生成器时出错:'无法检索'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; }
}
}
【问题讨论】:
标签: c# entity-framework asp.net-mvc-4 asp.net-identity entity-framework-migrations