问题描述
我正在开发一个简单的应用程序,允许用户创建一个配置文件并上传多张图片。我使用ASP.NET身份,因此将参考用户 ApplicationUser
。我有一个图片
和下方所示的 ApplicationUser
域模型:
公共类ApplicationUser:IdentityUser
{
公共异步任务< ClaimsIdentity> GenerateUserIdentityAsync(的UserManager< ApplicationUser>经理)
{
//注意authenticationType必须CookieAuthenticationOptions.AuthenticationType定义的匹配
VAR的UserIdentity =等待manager.CreateIdentityAsync(这一点,DefaultAuthenticationTypes.ApplicationCookie);
//添加自定义的用户在这里声明
返回的UserIdentity;
}
公共字符串USERFIRSTNAME {搞定;组; }
公共字符串UserSurname {搞定;组; }
公共字符串城{搞定;组; }
公共虚拟的ICollection<图像>图片{搞定;组; }
}
和
公共类图片
{
公众诠释ImageID {搞定;组; }
公共字符串ImageCaption {搞定;组; }
公众诠释NumOfLikes {搞定;组; }
公共字符串ImageFilePath {搞定;组; }
// [ForeignKey的(ApplicationUser)]
//公众诠释ApplicationUserID {搞定;组; }
公共虚拟ApplicationUser ApplicationUser {搞定;组; }
}
我添加 public虚拟的ICollection<图像>图片{搞定;组; }
和 public虚拟ApplicationUser ApplicationUser {搞定;组; }
来定义 ApplicationUser
之间的一到多的关系图片
。运行该应用程序后,我注意到,实体框架已在图片表中创建 ApplicationUser_id
(我假设我可以作为外键使用,而不必取消对code在在图片
域模型)。
我也有 ViewImagesViewModel
将返回到一个视图,图像的集合,我希望能够过滤和排序 ApplicationUser
的名字,姓氏和城市。我的问题是我不知道如何从域模型的属性组合成一个单一的视图模型。
问题
- 如何访问或返回
ApplicationUser
返回时的属性图片
? - 如何绑定
图像的相关属性,如
ApplicationUser
的USERFIRSTNAME
到ViewImagesViewModel
?
要获取所需的数据即时加载到一个模型中,可以使用包含
当获取的图像,还可以获取用户:
VAR图像= context.Images.Include(I => i.ApplicationUser).FirstOrDefault(I => i.ImageId == imageId);
或相反,获取用户,当你可以获取该用户的所有图片:
VAR用户= context.ApplicationUser.Include(U => u.Image).FirstOrDefault(U => u.Id ==用户id);
在这之后,您可以访问 image.ApplicationUser.UserSurname
。请参见
I'm developing a simple application that will allow users to create a profile and upload multiple images. I'm using ASP.NET Identity and will therefore refer to "user" as ApplicationUser
. I have an Image
and an ApplicationUser
domain model shown below:
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 string UserFirstName { get; set; }
public string UserSurname { get; set; }
public string City { get; set; }
public virtual ICollection<Image> Image { get; set; }
}
And
public class Image
{
public int ImageID { get; set; }
public string ImageCaption { get; set; }
public int NumOfLikes { get; set; }
public string ImageFilePath { get; set; }
//[ForeignKey("ApplicationUser")]
//public int ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
I added public virtual ICollection<Image> Image { get; set; }
and public virtual ApplicationUser ApplicationUser { get; set; }
to define a one-to-many relationship between ApplicationUser
and Image
. After running the application I noticed that Entity Framework had created ApplicationUser_id
in the Images table (Which I assume I can use as a foreign key without having to uncomment the code in the Image
domain model).
I also have ViewImagesViewModel
which will return to a view, a collection of images which I would like to be able to filter and sort by ApplicationUser
's name,surname and city. My problem is that I don't know how to combine the properties from both domain models into a single view model.
Questions
- How do I access or return
ApplicationUser
's properties when returningImage
? - How do I bind
Image
's related attributes such asApplicationUser
'sUserFirstName
toViewImagesViewModel
?
To get the required data 'eagerly loaded' into one model, you can use Include
When fetching an image, you can also fetch the user:
var image = context.Images.Include(i => i.ApplicationUser).FirstOrDefault(i => i.ImageId == imageId);
or the reverse, when fetching a user you can fetch all images for that user:
var user = context.ApplicationUser.Include(u => u.Image).FirstOrDefault(u => u.Id == userId);
After that, you can access image.ApplicationUser.UserSurname
. See this MSDN article
这篇关于你如何使用Entity Framework的访问模式相关的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!