有人可以帮我了解如何使用ViewModel将数据保存和更新到多个实体吗?
我有一个看起来像这样的ViewModel:
public class StudentViewModel
{
public Student student;
public StudentAddress studentAddress { get; set; }
public StudentPhoto studentPhoto { get; set; }
// Three entities are related to one to one relationship
public DoctorDetailsViewModel()
{ }
}
我的控制器是:
[HttpPost]
public ActionResult Create(StudentViewModel studentViewModel)
{
if (ModelState.IsValid)
{
return View(studentViewModel);
}
Student s = new Student()
{
Name =studentViewModel.Student.Name,
Speciality = studentViewModel.Student.Speciality,
DateOfJoinig = studentViewModel.Student.DateOfJoinig,
Qualification = studentViewModel.Student.Qualification,
Email = studentViewModel.Student.Email
};
StudentAddress sa = new StudentAddress()
{
StudentId= studentViewModel.Student.StudentId,
Address = studentViewModel.StudentAddress.Address,
Area = studentViewModell.StudentAddress.Area,
City = studentViewModel.StudentAddress.City,
State = studentViewModel.StudentAddress.State,
Pincode = studentViewModel.StudentAddress.Pincode,
Phone = studentViewModel.StudentAddress.Phone,
Mobile = studentViewModel.StudentAddress.Mobile
};
StudentPhoto sp = new StudentPhoto()
{
StudentId= studentViewModel.Student.StudentId,
Photo = studentViewModel.StudentPhoto.Photo
};
db.Students.Add(s);
db.StudentAddress.Add(sa);
db.StudentPhoto.Add(sp);
db.SaveChanges();
return RedirectToAction("Home");
}
我能够检索并将数据(来自多个实体)显示到视图中。但是,现在我仍然坚持如何使用新数据保存和更新上述实体。大多数示例都是1-1关系,映射是自动的,但在这种情况下,数据属于多个实体。
最好的方法是什么?感谢。
答案 0 :(得分:2)
首先,您的模型应如下所示:
public class Student
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int StudentID { get; set; }
public string Speciality { get; set; }
public DateTime DateOfJoinig { get; set; }
public string Qualification { get; set; }
public string Email { get; set; }
}
public class StudentAddress
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int StudentAddressID { get; set; }
[Required]
[ForeignKey("Student")]
public int StudentID { get; set; }
public virtual Student Student { get; set; }
public string Address { get; set; }
public string Area { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Pincode { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
}
public class StudentPhoto
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int StudentPhotoID { get; set; }
[Required]
[ForeignKey("Student")]
public int StudentID { get; set; }
public virtual Student Student { get; set; }
public string Photo { get; set; }
}
正如卡齐提到的,你应该有导航属性。 然后在客户端,您应该为ID属性隐藏字段以进行编辑:
@Html.HiddenFor(s => s.StudentID)
之后你的控制器方法将如下所示:
[HttpPost]
public ActionResult Create(StudentViewModel studentViewModel)
{
if (!ModelState.IsValid)
{
return View(studentViewModel);
}
studentViewModel.StudentAddress.Student = studentViewModel.Student;
studentViewModel.StudentPhoto.Student = studentViewModel.Student;
if (studentViewModel.Student.StudentID > 0)
db.Students.Attach(studentViewModel.Student);
else
db.Students.Add(studentViewModel.Student);
db.SaveChanges();
return RedirectToAction("Home");
}
您无需指定ID属性,但需要指定导航属性。
答案 1 :(得分:0)
未经测试,但这可能会有所帮助
[HttpPost]
public ActionResult Create(StudentViewModel studentViewModel)
{
if (!ModelState.IsValid) // added ! so that you can do something if it is valid other than redisplay the Create View with the model
{
return View(studentViewModel);
}
Student s = new Student()
{
Name =studentViewModel.Student.Name,
Speciality = studentViewModel.Student.Speciality,
DateOfJoinig = studentViewModel.Student.DateOfJoinig,
Qualification = studentViewModel.Student.Qualification,
Email = studentViewModel.Student.Email
};
db.Students.Add(s);
db.SaveChanges(); // Post the changes with the student and save changes so you have the correct studentId PrimaryKey value from the database. Note: EF should update your object with the correct Id on SaveChanges()
StudentAddress sa = new StudentAddress()
{
StudentId= studentViewModel.Student.StudentId,
Address = studentViewModel.StudentAddress.Address,
Area = studentViewModell.StudentAddress.Area,
City = studentViewModel.StudentAddress.City,
State = studentViewModel.StudentAddress.State,
Pincode = studentViewModel.StudentAddress.Pincode,
Phone = studentViewModel.StudentAddress.Phone,
Mobile = studentViewModel.StudentAddress.Mobile
};
StudentPhoto sp = new StudentPhoto()
{
StudentId= studentViewModel.Student.StudentId,
Photo = studentViewModel.StudentPhoto.Photo
};
// don't add it again! db.Students.Add(s);
db.StudentAddress.Add(sa);
db.StudentPhoto.Add(sp);
db.SaveChanges();
return RedirectToAction("Home");
}
答案 2 :(得分:0)
在使用关系数据库时,我理解studentaddress将是Student的子级,如StudentAddress和StudentPhoto表中的StudentId外键所示。如果向Student添加导航属性并为这些属性分配地址和照片,Entity框架将保存它们。像
这样的东西Student s = new Student {...}
s.StudentAddress = new StudentAddress {...}
s.StudentPhoto = new StudentPhoto {...}
db.Students.Add (s);
db.SaveChanges();