C# 如何使用实体框架访问与模型相关的属性?
我正在开发一个简单的应用程序,允许用户创建一个配置文件并上传多个图像。我使用的是ASP.NET标识,因此将“用户”称为C# 如何使用实体框架访问与模型相关的属性?,c#,asp.net,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Identity,我正在开发一个简单的应用程序,允许用户创建一个配置文件并上传多个图像。我使用的是ASP.NET标识,因此将“用户”称为ApplicationUser。我有一个图像和一个应用程序用户域模型,如下所示: public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<Applica
ApplicationUser
。我有一个图像
和一个应用程序用户
域模型,如下所示:
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; }
}
我添加了public-virtualicollection-Image{get;set;}
和public-virtualapplicationuser-ApplicationUser{get;set;}
来定义ApplicationUser
和Image
之间的一对多关系。运行应用程序后,我注意到Entity Framework在Images表中创建了ApplicationUser\u id
(我假设可以将其用作外键,而无需在Image
域模型中取消对代码的注释)。,我希望能够根据
ApplicationUser
的姓名、姓氏和城市筛选和排序的图像集合。我的问题是,我不知道如何将两个域模型的属性组合到一个视图模型中。问题
Image
的相关属性(如ApplicationUser
的UserFirstName
绑定到viewmagesviewmodel
李>
要将所需数据“急切地加载”到一个模型中,您可以使用
Include
获取图像时,还可以获取用户:
var image = context.Images.Include(i => i.ApplicationUser).FirstOrDefault(i => i.ImageId == imageId);
反之,获取用户时,您可以获取该用户的所有图像:
var user = context.ApplicationUser.Include(u => u.Image).FirstOrDefault(u => u.Id == userId);
之后,您可以访问
image.ApplicationUser.username
。请参见要将所需数据“急切加载”到一个模型中,您可以使用Include
获取图像时,还可以获取用户:
var image = context.Images.Include(i => i.ApplicationUser).FirstOrDefault(i => i.ImageId == imageId);
反之,获取用户时,您可以获取该用户的所有图像:
var user = context.ApplicationUser.Include(u => u.Image).FirstOrDefault(u => u.Id == userId);
之后,您可以访问
image.ApplicationUser.username
。请参见要将所需数据“急切加载”到一个模型中,您可以使用Include
获取图像时,还可以获取用户:
var image = context.Images.Include(i => i.ApplicationUser).FirstOrDefault(i => i.ImageId == imageId);
反之,获取用户时,您可以获取该用户的所有图像:
var user = context.ApplicationUser.Include(u => u.Image).FirstOrDefault(u => u.Id == userId);
之后,您可以访问
image.ApplicationUser.username
。请参见要将所需数据“急切加载”到一个模型中,您可以使用Include
获取图像时,还可以获取用户:
var image = context.Images.Include(i => i.ApplicationUser).FirstOrDefault(i => i.ImageId == imageId);
反之,获取用户时,您可以获取该用户的所有图像:
var user = context.ApplicationUser.Include(u => u.Image).FirstOrDefault(u => u.Id == userId);
之后,您可以访问
image.ApplicationUser.username
。请参见正如您所说,您有一个视图图像视图模型
,
所以我认为这个视图模型应该是这样的
public class ViewImagesViewModel
{
public void ViewImagesViewModel()
{
this.Images = new List<ViewImageViewModel>();
}
public string UserFirstName { get; set; }
public string UserSurname { get; set; }
public string City { get; set; }
public List<ViewImageViewModel> Images { get; set; }
}
public class ViewImageViewModel
{
public string ImageUrl { get; set; }
public int Likes { get; set; }
public string Caption { get; set; }
}
...
var entity = db.ApplicationUsers.FirstOrDefault(x => x.Id == 1);
if (entity != null)
{
var model = new ViewImagesViewModel();
model.UserFirstName = entity.UserFirstName;
model.UserSurname = entity.UserSurname;
model.City = entity.City;
// fix the Image property in the ApplicationUser entity to be Images instead of Image as it represent a collection
foreach (var img in entity.Images)
{
var imgModel = new ViewImageViewModel()
{
// you may change this to get the correct Url
ImageUrl = string.Concat(HttpContext.Request.Url.Scheme,
"://", HttpContext.Request.Url.Host,
HostingEnvironment.ApplicationVirtualPath,
img.ImageFilePath),
Likes = img.NumOfLikes,
Caption = img.Caption
};
model.Images.Add(imgModel);
}
return View(model);
}
...
正如您所说,您有一个
视图imagesviewmodel
,
所以我认为这个视图模型应该是这样的
public class ViewImagesViewModel
{
public void ViewImagesViewModel()
{
this.Images = new List<ViewImageViewModel>();
}
public string UserFirstName { get; set; }
public string UserSurname { get; set; }
public string City { get; set; }
public List<ViewImageViewModel> Images { get; set; }
}
public class ViewImageViewModel
{
public string ImageUrl { get; set; }
public int Likes { get; set; }
public string Caption { get; set; }
}
...
var entity = db.ApplicationUsers.FirstOrDefault(x => x.Id == 1);
if (entity != null)
{
var model = new ViewImagesViewModel();
model.UserFirstName = entity.UserFirstName;
model.UserSurname = entity.UserSurname;
model.City = entity.City;
// fix the Image property in the ApplicationUser entity to be Images instead of Image as it represent a collection
foreach (var img in entity.Images)
{
var imgModel = new ViewImageViewModel()
{
// you may change this to get the correct Url
ImageUrl = string.Concat(HttpContext.Request.Url.Scheme,
"://", HttpContext.Request.Url.Host,
HostingEnvironment.ApplicationVirtualPath,
img.ImageFilePath),
Likes = img.NumOfLikes,
Caption = img.Caption
};
model.Images.Add(imgModel);
}
return View(model);
}
...
正如您所说,您有一个
视图imagesviewmodel
,
所以我认为这个视图模型应该是这样的
public class ViewImagesViewModel
{
public void ViewImagesViewModel()
{
this.Images = new List<ViewImageViewModel>();
}
public string UserFirstName { get; set; }
public string UserSurname { get; set; }
public string City { get; set; }
public List<ViewImageViewModel> Images { get; set; }
}
public class ViewImageViewModel
{
public string ImageUrl { get; set; }
public int Likes { get; set; }
public string Caption { get; set; }
}
...
var entity = db.ApplicationUsers.FirstOrDefault(x => x.Id == 1);
if (entity != null)
{
var model = new ViewImagesViewModel();
model.UserFirstName = entity.UserFirstName;
model.UserSurname = entity.UserSurname;
model.City = entity.City;
// fix the Image property in the ApplicationUser entity to be Images instead of Image as it represent a collection
foreach (var img in entity.Images)
{
var imgModel = new ViewImageViewModel()
{
// you may change this to get the correct Url
ImageUrl = string.Concat(HttpContext.Request.Url.Scheme,
"://", HttpContext.Request.Url.Host,
HostingEnvironment.ApplicationVirtualPath,
img.ImageFilePath),
Likes = img.NumOfLikes,
Caption = img.Caption
};
model.Images.Add(imgModel);
}
return View(model);
}
...
正如您所说,您有一个
视图imagesviewmodel
,
所以我认为这个视图模型应该是这样的
public class ViewImagesViewModel
{
public void ViewImagesViewModel()
{
this.Images = new List<ViewImageViewModel>();
}
public string UserFirstName { get; set; }
public string UserSurname { get; set; }
public string City { get; set; }
public List<ViewImageViewModel> Images { get; set; }
}
public class ViewImageViewModel
{
public string ImageUrl { get; set; }
public int Likes { get; set; }
public string Caption { get; set; }
}
...
var entity = db.ApplicationUsers.FirstOrDefault(x => x.Id == 1);
if (entity != null)
{
var model = new ViewImagesViewModel();
model.UserFirstName = entity.UserFirstName;
model.UserSurname = entity.UserSurname;
model.City = entity.City;
// fix the Image property in the ApplicationUser entity to be Images instead of Image as it represent a collection
foreach (var img in entity.Images)
{
var imgModel = new ViewImageViewModel()
{
// you may change this to get the correct Url
ImageUrl = string.Concat(HttpContext.Request.Url.Scheme,
"://", HttpContext.Request.Url.Host,
HostingEnvironment.ApplicationVirtualPath,
img.ImageFilePath),
Likes = img.NumOfLikes,
Caption = img.Caption
};
model.Images.Add(imgModel);
}
return View(model);
}
...
您可以显示
viewmagesviewmodel
代码吗?还可以显示控制器中的代码您可以显示viewmagesviewmodel
代码吗?还可以显示控制器中的代码您可以显示viewmagesviewmodel
代码吗?还可以显示控制器中的代码您可以显示viewmagesviewmodel
代码吗?还可以显示我们控制器中的代码告诉我,如果我错了,我是asp.net的新手,仍然掌握着基本知识。您的控制器似乎是我用来获取单个用户的所有图像的控制器。我的问题是,我不知道/不知道如何返回包含相关用户名、姓氏和城市的所有图像。我认为我必须在我的图像
域模型中包含这些属性,以映射每个图像与其所有者的详细信息之间的关系。如果我错了,请原谅,我对asp.net相当陌生,并且仍然掌握基本知识。您的控制器似乎是我用来获取单个用户的所有图像的控制器。我的问题是,我不知道/不知道如何返回包含相关用户名、姓氏和城市的所有图像。我认为我必须在我的图像
域模型中包含这些属性,以映射每个图像与其所有者的详细信息之间的关系。如果我错了,请原谅,我对asp.net相当陌生,并且仍然掌握基本知识。您的控制器似乎是我用来获取单个用户的所有图像的控制器。我的问题是,我不知道/不知道如何返回包含相关用户名、姓氏和城市的所有图像。我认为我必须在我的图像
域模型中包含这些属性,以映射每个图像与其所有者的详细信息之间的关系。如果我错了,请原谅,我对asp.net相当陌生,并且仍然掌握基本知识。您的控制器似乎是我用来获取单个用户的所有图像的控制器。我的问题是,我不知道/不知道如何返回包含相关用户名、姓氏和城市的所有图像。我想我必须在我的图像
域模型中包含这些属性,以映射每个图像与其所有者的详细信息之间的关系。在您提供的示例中,如何访问图像user.Image[0].ImageId
?可以使用foreach(user.Image中的var Image){}
列出所有图像,或者使用user.Image.FirstOrDefault()
获取该用户的第一个图像,或者如果该用户没有图像,则使用null
。查看如何访问您提供的示例中的图像user.Image[0].ImageId
?可以使用foreach(user.Image中的var Image){}
列出所有图像,也可以使用user.Image.FirstOrDefault()
获取第一个