如何将应用程序用户置于与其他对象相同的上下文中? [英] How can one put application users in the same context as the rest of the objects?

查看:29
本文介绍了如何将应用程序用户置于与其他对象相同的上下文中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The stock asp.net mvc 5 application creates the application users, aka identity users in a separate context, named a file called "IdentityModels.cs" - it looks like this

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
}

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

I am attempting to put the Application users in a regular data context, i.e. something like this

 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; }
    }

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

However, every time I do that I get the following error every time I try to create an account

The UserId field is required

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

In the AccountController.cs when I attempt to execure the following line of code

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

我觉得我的方法是错误的,如果没有某种外部欺骗,我就不能在主数据上下文文件中使用 ApplicationUsers - 有没有人知道这样做的方法?所有文件都是最新的.

I get the feeling my approach is wrong, and that I cannot have the ApplicationUsers in the main data context file without some sort of external chicanery - does anyone know of some way to do this? All of the files are up to date.

推荐答案

这有点太简单了 - 事实证明,您只需删除

This was a bit too easy - it turns out that all you have to do it remove the

<ApplicationUser> 

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

when you call the context and everything turns out like you might expect (i.e. the MVC assumptions and the developers assumptions (in this case, mine) sync.

这里工作正常

 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>();
        }
    }

这篇关于如何将应用程序用户置于与其他对象相同的上下文中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆