本文介绍了如何在ASP.NET MVC 5 Identity 2.0中使用ASP.NET MVC 5 Identity 2.0中的AspNetUser表(Id列)的Customer表(CreatedBy列)中添加外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经使用Visual Studio 2013 Update 2 RC & amp;然后添加AspNet身份样本:I have created Empty MVC (ASP.NET Web Application) project using Visual Studio 2013 Update 2 RC& then added AspNet Identity Samples using:我启用并添加了迁移和然后更新数据库,创建默认表。I have enabled and added migrations & then updated database, which created default tables. I要创建包含2列作为外键的Customer表:I want to create Customer table which contains 2 columns as Foreign Keys to: 组表(GroupId列) AspNetUsers表(Id列)所以我创建了2个类Customer&使用数据注释组合并添加外键,如下所示:So I have created 2 classes Customer & Group and added Foreign Keys using Data-annotations as shown below:namespace IdentitySample.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 class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } static ApplicationDbContext() { // Set the database intializer which is run once during application start // This seeds the database with admin user credentials and admin role Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer()); } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } public DbSet<Customer> Customers { get; set; } public DbSet<Group> Groups { get; set; } } public class Customer { public int CustomerId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public int GroupId { get; set; } public string CreatedBy { get; set; } [ForeignKey("GroupId")] public Group Groups { get; set; } [ForeignKey("CreatedBy")] public ApplicationUser ApplicationUsers { get; set; } } public class Group { public int GroupId { get; set; } public string GroupName { get; set; } }}一切似乎都很好,ApplicationDbContext.edmx图创建使用EF电动工具也看起来很好项目正确构建。Everything seems fine, the ApplicationDbContext.edmx diagram created using EF Power tools also looks fine & project builds properly.然后,我使用MVC 5控制器的视图,使用实体框架脚手架模板添加了CustomersController。I have then added CustomersController using "MVC 5 Controller with views, using Entity Framework" scaffolding template. img src =https://i.stack.imgur.com/WGFGF.pngalt =enter image description here>现在我正在编译错误(在db.ApplicationUsers)Now I am getting compilation error (at db.ApplicationUsers)// GET: Customers/Createpublic ActionResult Create(){ ViewBag.CreatedBy = new SelectList(db.ApplicationUsers, "Id", "Email"); ViewBag.GroupId = new SelectList(db.Groups, "GroupId", "GroupName"); return View();}错误详细信息: 'IdentitySample.Models.ApplicationDbContext'不包含'ApplicationUsers'的定义和没有扩展方法'ApplicationUsers'接受类型'IdentitySample.Models.ApplicationDbContext'的第一个参数可以找到(你缺少一个using指令或程序集引用吗?)当我在 ApplicationDbContext 中添加以下代码public DbSet<ApplicationUser> ApplicationUsers { get; set; }我收到错误: $ b $的实例b 我想添加CreatedBy作为AspNetUsers的外键,使用该模板生成类似于GroupId下拉列表的CreatedBy下拉列表: 我们如何使用身份生成的表与其他用户创建的表,其中用户创建的表具有对Identity生成的表的外键引用。并且具有使用MVC 5控制器与视图,使用实体框架的一级脚手架代码生成体验? (类似于我们对Customers& Groups表的使用,客户表具有GroupId外键引用)推荐答案 RTFE:您没有属性ApplicationUsers在您的 ApplicationDbContext class。RTFE: you do not have a property ApplicationUsers on your ApplicationDbContext class.只需添加: public DbSet<ApplicationUser> ApplicationUsers { get; set; } 这篇关于如何在ASP.NET MVC 5 Identity 2.0中使用ASP.NET MVC 5 Identity 2.0中的AspNetUser表(Id列)的Customer表(CreatedBy列)中添加外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-15 07:46