gpt4 book ai didi

c# - 如何使用 Code First 在 ASP.NET MVC 5 Identity 2.0 中为 AspNetUser 表(Id 列)的客户表(CreatedBy 列)添加外键

转载 作者:太空狗 更新时间:2023-10-30 01:04:28 26 4
gpt4 key购买 nike

我使用 Visual Studio 2013 Update 2 RC 创建了空 MVC(ASP.NET Web 应用程序)项目& 然后使用以下方法添加了 AspNet Identity Samples:

PM>Install-Package Microsoft.AspNet.Identity.Samples -Pre

我启用并添加了迁移,然后更新了创建默认表的数据库。 enter image description here

我想创建包含 2 列作为外键的 Customer 表:

  • 组表(GroupId 列)
  • AspNetUsers 表(Id 列)

所以我创建了 2 个类 Customer 和 Group,并使用数据注释添加了外键,如下所示:

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 图看起来也很好,项目构建正确。

enter image description here

然后我使用“MVC 5 Controller with views, using Entity Framework”脚手架模板添加了 CustomersController。

enter image description here

现在我收到编译错误(在 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();
}

enter image description here

错误详情:“IdentitySample.Models.ApplicationDbContext”不包含“ApplicationUsers”的定义,并且找不到接受“IdentitySample.Models.ApplicationDbContext”类型的第一个参数的扩展方法“ApplicationUsers”(您是否缺少 using 指令或汇编引用?)

当我在 ApplicationDbContext 中添加以下代码时

public DbSet<ApplicationUser> ApplicationUsers { get; set; }

我得到错误:

Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'IdentitySample.Models.ApplicationUser'

我想将 CreatedBy 作为外键添加到 AspNetUsers,模板使用它生成类似于 GroupId 下拉列表的 CreatedBy 下拉列表:

enter image description here

我们如何将身份生成的表与其他用户创建的表一起使用,其中用户创建的表具有对身份生成的表的外键引用。并拥有使用“带 View 的 MVC 5 Controller ,使用 Entity Framework ”的一流脚手架代码生成经验?
(类似于我们的 Customers & Groups 表,其中 Customers 表具有 GroupId 外键引用)

最佳答案

Entity Framework 足够智能,可以识别 ID/键的常规名称。因此,为了添加外键,您可以这样做:

public class Customer
{
public string CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }

public int ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}

按照惯例,它匹配一个类名加上“Id”作为外键。如果您不想使用此约定,则可以使用 ForeignKey 数据注释来帮助 EF 了解外键应该是什么:

public class Customer
{
public string CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }

public int UserId { get; set; }
[ForeignKey("UserId")]
public ApplicationUser ApplicationUser { get; set; }
}

看看thisthis文章了解更多信息。

关于c# - 如何使用 Code First 在 ASP.NET MVC 5 Identity 2.0 中为 AspNetUser 表(Id 列)的客户表(CreatedBy 列)添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23119641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com