【问题标题】:How can one put application users in the same context as the rest of the objects?如何将应用程序用户置于与其他对象相同的上下文中?
【发布时间】:2013-11-06 19:59:02
【问题描述】:

股票 asp.net mvc 5 应用程序在单独的上下文中创建应用程序用户,也就是身份用户,命名一个名为“IdentityModels.cs”的文件 - 它看起来像这样

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
}

我正在尝试将应用程序用户置于常规数据上下文中,即类似这样的内容

 public class BlogProphetContext : DbContext
    {

        public DbSet<ApplicationUser> ApplicationUsers { get; set; }
        public DbSet<Answer> Answers { get; set; }
        public DbSet<Question> Questions { get; set; }
        public DbSet<Tag> Tags { get; set; }
    }

但是,每次我尝试创建帐户时都会收到以下错误

The UserId field is required

当我尝试执行以下代码行时,在 AccountController.cs 中

result = await UserManager.AddLoginAsync(user.Id, info.Login);

我觉得我的方法是错误的,如果没有某种外部诡计,我无法在主数据上下文文件中包含 ApplicationUsers - 有人知道这样做的方法吗?所有文件都是最新的。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5 asp.net-identity


    【解决方案1】:

    我不确定这是否会对你有所帮助,或者我是否完全理解你的问题......但如果你想尝试使用你自己的“模型”然后改变:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("PUT IN YOUR MODEL CONN HERE")
        {
        }
    

    我不知道您可以配置多少,或者这是否真的会改变上下文。

    【讨论】:

      【解决方案2】:

      确保您的 ApplicationUser 类继承自 IdentityUser。

      然后您需要配置您的上下文,以便它正确映射所有 ASP.NET 标识类。 它们是:

      1. ApplicationUser(你的班级)
      2. IdentityUserClaim
      3. 身份用户登录
      4. 身份用户角色
      5. 身份角色

      这里发布的细节有点太复杂了,但我已经制作了一个示例项目,它适用于 ASP.NET Identity 的发布版本。你可以在这里看到它: https://github.com/onybo/Asp.Net-Identity-sample-app/tree/master/CustomUser

      【讨论】:

      • 我实际上根本没有使用 IdentityDbContext,我想像对待任何其他类一样对待 ApplicationUser (扩展和相关属性等 - 它奇怪地在 beta 中工作) 我想知道是否有任何方法根本不使用 IdentityDBContext。
      • 我确实拥有最新版本的所有内容 - 我现在正在更新问题。
      • 好的,我已经更新了我的答案,并包含了一个我认为可以满足您的需求的工作项目的链接
      【解决方案3】:

      在这种情况下,如果您不希望 ApplicationUser 与 asp.net Identity 相关联,最简单的方法就是删除 applicationUser 类的继承。

      从 ApplicationUser : IdentityUser 转到 ApplicationUser,然后在该类中创建一个 Id 属性。

      那么你就不必使用 IdentityDBContext 了。

      当然,您必须完全重写 AccountController,或者摆脱它。无论如何,您都不能使用 UserManager 作为链接到 IdentityUser

      如果您使用的是 EF,那么显然请确保添加您的迁移/更新数据库。

      如果您确实希望保持帐户控制器不变并保留 IdentityDbContext 但添加其他实体,那么这很容易,只需执行以下操作:

      public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
      {
          public ApplicationDbContext() : base("DefaultConnection")
          {
          }
      
          public DbSet<Dept> Dept{ get; set; }
          public DbSet<Course> Course{ get; set; }
          public DbSet<Etc> Etc{ get; set; }
      }
      

      【讨论】:

        【解决方案4】:

        这有点太容易了 - 事实证明,你所要做的就是删除

        <ApplicationUser> 
        

        当您调用上下文时,一切结果都如您所料(即 MVC 假设和开发人员假设(在本例中为我的假设)同步。

        这里工作正常

         public class MyContext : IdentityDbContext
            {
                public MyContext()
                    : base("DefaultConnection")
                {
                }
                public DbSet<ApplicationUser> ApplicationUsers { get; set; }
                public DbSet<Answer> Answers { get; set; }
        
                protected override void OnModelCreating(DbModelBuilder modelBuilder)
                {
                    base.OnModelCreating(modelBuilder);
                    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
                }
            }
        

        【讨论】:

        • 救命稻草,这也困扰着我。我讨厌有两个上下文的想法:一个用于身份子系统,另一个用于我的项目的所有其他数据!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多