Asp.net mvc 3 使用具有两个相关实体的ViewModel创建
我拥有ViewModel中两个实体的属性。这两个实体都是相互关联的,例如,User和Posts。每个用户可以有多个帖子,许多帖子可以属于单个用户(一对多) 我的ViewModel的目的是允许在同一表单上添加用户和帖子。因此,我的ViewModel如下所示:Asp.net mvc 3 使用具有两个相关实体的ViewModel创建,asp.net-mvc-3,entity-framework-4.1,Asp.net Mvc 3,Entity Framework 4.1,我拥有ViewModel中两个实体的属性。这两个实体都是相互关联的,例如,User和Posts。每个用户可以有多个帖子,许多帖子可以属于单个用户(一对多) 我的ViewModel的目的是允许在同一表单上添加用户和帖子。因此,我的ViewModel如下所示: public class CreateVM { [Required, MaxLength(50)] public string Username { get; set; } [Required, MaxLength(
public class CreateVM
{
[Required, MaxLength(50)]
public string Username { get; set; }
[Required, MaxLength(500), MinLength(50)]
public string PostBody { get; set; }
// etc with some other related properties
}
[HttpPost]
public ActionResult Create(CreateVM vm)
{
if (ModelState.IsValid)
{
User u = new User()
{
Username = vm.Username,
// etc populate properties
};
Post p = new Post()
{
Body = vm.PostBody,
// etc populating properties
};
p.User = u; // Assigning the new user to the post.
XContext.Posts.Add(p);
XContext.SaveChanges();
}
}
在Create方法的控制器中,我有如下内容:
public class CreateVM
{
[Required, MaxLength(50)]
public string Username { get; set; }
[Required, MaxLength(500), MinLength(50)]
public string PostBody { get; set; }
// etc with some other related properties
}
[HttpPost]
public ActionResult Create(CreateVM vm)
{
if (ModelState.IsValid)
{
User u = new User()
{
Username = vm.Username,
// etc populate properties
};
Post p = new Post()
{
Body = vm.PostBody,
// etc populating properties
};
p.User = u; // Assigning the new user to the post.
XContext.Posts.Add(p);
XContext.SaveChanges();
}
}
当我通过调试器浏览帖子时,一切看起来都很好,但是当我试图查看帖子时,它的用户关系为空
我也试过了
u.Posts.Add(p);
更新:
我的邮政编码如下:
public class Post
{
[Key]
public int Id { get; set; }
[Required, MaxLength(500)]
public string Body { get; set; }
public int Likes { get; set; }
[Required]
public bool isApproved { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
[Required]
public User User { get; set; }
}
但这也不起作用。我做错了什么?问题是EF不能延迟加载
用户
属性,因为您没有将其设置为虚拟
public class Post
{
[Key]
public int Id { get; set; }
[Required, MaxLength(500)]
public string Body { get; set; }
public int Likes { get; set; }
[Required]
public bool isApproved { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
[Required]
public virtual User User { get; set; }
}
如果您事先知道要访问帖子的User
属性,则应立即加载与帖子相关的User
context.Posts.Include("User").Where(/* condition*/);
问题是EF无法延迟加载
用户
属性,因为您尚未将其设置为虚拟
public class Post
{
[Key]
public int Id { get; set; }
[Required, MaxLength(500)]
public string Body { get; set; }
public int Likes { get; set; }
[Required]
public bool isApproved { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
[Required]
public virtual User User { get; set; }
}
如果您事先知道要访问帖子的User
属性,则应立即加载与帖子相关的User
context.Posts.Include("User").Where(/* condition*/);
您能显示
Post
class代码吗?请参阅更新的Post。谢谢,你能展示一下Post
课程代码吗?请参阅更新的Post。感谢ErangaThanks Eranga,我认为这不会是一个问题,因为我没有从数据库加载用户(或者我是吗?),因为用户是在创建帖子的同时创建的。我会把它改成虚拟的,看看会发生什么。谢谢Eranga,我不认为这会是一个问题,因为我没有从数据库加载用户(或者我是吗?),因为用户是在创建帖子的同时创建的。我会把它改成虚拟的,看看会发生什么。我会发回的。