我正在关注 this tutorial创建帐户后添加个人资料图片。
该编辑有以下片段,
Student student = db.Students.Include(s => s.Files).SingleOrDefault(s => s.ID == id);
在上面的示例中,它应用于表调用 Student
在我的例子中我想使用 AspNetUser
表,但是我必须使用 UserManger
功能,一旦我尝试 Include
一个文件,它就会在编译时弹出该错误。
但在我的场景中,我试图包含在 AspNetUser
或 UserManager
所以要填充上传的图片。我需要在用户管理器中包含
以下代码
var user = await UserManager.Include(s => s.Files).FindByIdAsync(userid);
但随后出现以下错误
'ApplicationUserManager' does not contain a definition for 'Include' and no extension method 'Include' accepting a first argument of type '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 中键入以下内容来向项目添加迁移:
添加迁移文件
添加迁移文件路径
然后在控制台中出现以下错误
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).
然后我在 PMC 中输入了以下内容:
启用迁移 project_name.Models.sampleEntityFrameworkEntities
然后我在控制台中得到以下错误
Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
然后我尝试使用以下代码迁移 Files
和 FilesPath
添加迁移文件
添加迁移文件路径
控制台报错
Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
最佳答案
如果我对你的理解是正确的,你正在尝试遵循指南,但使用的是 ASP.NET Identity。 ASP.NET Identity 是一个独特的系统,有自己的类和 DBContext。为了向用户添加新字段,您需要修改 ApplicationUser 类。只需将您的属性添加到此类并在需要时应用迁移。这将向 AspNetUser 添加一个新列。然后将您的字段添加到 ViewModel(如果有)以及与您的字段一起工作所需的其他逻辑。
关于c# - 使用 usermanager 编辑用户个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34918461/