【问题标题】:edit user profile image with usermanager使用 usermanager 编辑用户个人资料图像
【发布时间】:2016-04-27 09:15:59
【问题描述】:

我正在关注this tutorial,以便在创建帐户后添加个人资料图片。

该编辑具有以下 sn-p,

Student student = db.Students.Include(s => s.Files).SingleOrDefault(s => s.ID == id);

在上面的示例中,它应用于表调用 Student 在我的情况下,我想使用 AspNetUser 表,但是我必须使用 UserManger 功能,一旦我尝试 Include 一个文件然后它在编译时弹出该错误。

但在我的场景中,我试图包含在 AspNetUserUserManager

所以要填充上传的图片。我需要include 用户管理器中的以下代码

var user = await UserManager.Include(s => s.Files).FindByIdAsync(userid);

但随后出现以下错误

“ApplicationUserManager”不包含“包含”的定义 并且没有扩展方法“包含”接受类型的第一个参数 '应用程序用户管理器'

如何忽略和包含文件

编辑:

这些是我在模型类中所做的更改

文件

public class File
{
    public int FileId { get; set; }
    ..

    public virtual ApplicationUser UserManager { get; set; }
}

文件路径

public class FilePath
{
    public int FilePathId { get; set; }
    ..

    public virtual ApplicationUser UserManager { get; set; }
}

应用程序用户

public class ApplicationUser : IdentityUser
{
        ....

        public virtual ICollection<File> Files { get; set; }
        public virtual ICollection<FilePath> FilePaths { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
       ....
    }
}

AspNetUser

public partial class AspNetUser
{

    public AspNetUser()
    {
       ..
    }
    ....

    public virtual ICollection<File> Files { get; set; }
    public virtual ICollection<FilePath> FilePaths { get; set; }
}

然后尝试通过在 PMC 中键入以下内容来将迁移添加到项目中:

add-migration File

add-migration FilePaths

然后在控制台中出现以下错误

在程序集中找不到迁移配置类型 '项目名'。 (在 Visual Studio 中,您可以使用 Enable-Migrations 来自包管理器控制台的命令以添加迁移 配置)。


然后我在 PMC 中输入了以下内容:

Enable-Migrations project_name.Models.sampleEntityFrameworkEntities

然后我在控制台中收到以下错误

创建 DbModelBuilder 或从创建的 DbContext 编写 EDMX 不支持使用 Database First 或 Model First。 EDMX 只能是 从 Code First DbContext 中获得,而无需使用现有的 DbCompiledModel。


然后我尝试使用以下代码迁移 FilesFilesPath

add-migration File

add-migration FilePaths

控制台报错

创建 DbModelBuilder 或从创建的 DbContext 编写 EDMX 不支持使用 Database First 或 Model First。 EDMX 只能是 从 Code First DbContext 中获得,而无需使用现有的 DbCompiledModel。

【问题讨论】:

  • 什么类型的UserManager
  • 给出错误的完整描述
  • 实际上在上面的例子中它适用于表调用 Student 在我的情况下我想使用 AspNetUser 表,但是我必须使用 UserManger 功能,一旦我尝试 Include它在编译时弹出该错误

标签: c# asp.net-mvc asp.net-mvc-3 nuget edmx


【解决方案1】:

如果我对您的理解正确,您正在尝试按照指南但使用 ASP.NET Identity。 ASP.NET Identity 是一个具有自己的类和 DBContext 的独特系统。为了向用户添加新字段,您需要修改 ApplicationUser 类。只需将您的属性添加到此类并在需要时应用迁移。这将向 AspNetUser 添加一个新列。然后将您的字段添加到 ViewModel(如果有)以及使用您的字段所需的其他逻辑。

【讨论】:

  • 实际上我在 IdentityModelAspNetUser 模型中的 ApplicationUser 下添加了以下行,public virtual ICollection&lt;File&gt; Files { get; set; }public virtual ICollection&lt;FilePath&gt; FilePaths { get; set; } 。然后如上所述更改行var user = await UserManager.Include(s =&gt; s.Files).FindByIdAsync(userid);,但这也会出错
  • 另外,我在 FileFilePathspublic virtual ApplicationUser UserManager { get; set; } 中添加了以下行,然后通过在 PMC 中键入以下内容向项目添加新迁移:add-migration FilePaths,然后出现以下错误No migrations configuration type was found in the assembly 'Project_Name'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
  • 嗯,很难说问题出在哪里。您可以发布您正在学习的所有课程吗?我的意思是 ApplicaitonUser、ApplicationDBContext 和 FilePath 以及 File。不是整个代码,而是每个类的一部分
猜你喜欢
  • 2020-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多