【发布时间】:2014-05-31 22:16:17
【问题描述】:
我使用 Visual Studio 2013 Update 2 RC 创建了 Empty MVC(ASP.NET Web 应用程序)项目 & 然后使用以下方法添加 AspNet 身份示例:
PM>安装包 Microsoft.AspNet.Identity.Samples -Pre
我已经启用并添加了迁移,然后更新了创建默认表的数据库。
我想创建包含 2 列作为外键的客户表:
- 组表(GroupId 列)
- AspNetUsers 表(Id 列)
所以我创建了 2 个类 Customer 和 Group 并使用 Data-annotations 添加了外键,如下所示:
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; }
}
}
一切看起来都很好,使用 EF Power 工具创建的 ApplicationDbContext.edmx 图表看起来也很好,并且项目构建正确。
然后我使用“MVC 5 Controller with views, using Entity Framework”脚手架模板添加了 CustomersController。
现在我收到编译错误(在 db.ApplicationUsers)
// GET: Customers/Create
public ActionResult Create()
{
ViewBag.CreatedBy = new SelectList(db.ApplicationUsers, "Id", "Email");
ViewBag.GroupId = new SelectList(db.Groups, "GroupId", "GroupName");
return View();
}
错误详情: “IdentitySample.Models.ApplicationDbContext”不包含“ApplicationUsers”的定义,并且找不到接受“IdentitySample.Models.ApplicationDbContext”类型的第一个参数的扩展方法“ApplicationUsers”(您是否缺少 using 指令或程序集参考?)
当我在ApplicationDbContext中添加以下代码时
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
我得到错误:
不支持每种类型的多个对象集。对象集 'ApplicationUsers' 和 'Users' 都可以包含类型的实例 'IdentitySample.Models.ApplicationUser'
我想将 CreatedBy 作为外键添加到 AspNetUsers,模板使用它生成类似于 GroupId 下拉列表的 CreatedBy 下拉列表:
我们如何将身份生成的表与其他用户创建的表一起使用,其中用户创建的表具有对身份生成表的外键引用。并且拥有使用“MVC 5 Controller with views, using Entity Framework”的一流脚手架代码生成经验?
(类似于我们的客户和组表,其中客户表具有 GroupId 外键参考)
【问题讨论】:
标签: c# asp.net entity-framework foreign-keys asp.net-identity