你如何使用Entity Framework的访问模式相关的属性? [英] How do you access a models related properties using Entity Framework?

查看:105
本文介绍了你如何使用Entity Framework的访问模式相关的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的应用程序,允许用户创建一个配置文件并上传多张图片。我使用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 的名字,姓氏和城市。我的问题是我不知道如何从域模型的属性组合成一个单一的视图模型。


问题


  1. 如何访问或返回 ApplicationUser 返回时的属性图片

  2. 如何绑定图像的相关属性,如 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 。请参见这个MSDN文章

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

  1. How do I access or return ApplicationUser's properties when returning Image?
  2. How do I bind Image's related attributes such as ApplicationUser's UserFirstName to ViewImagesViewModel?

解决方案

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的访问模式相关的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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