0

有人可以帮助我如何使用 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 关系映射是自动的,但在这种情况下,数据属于多个实体。

做这个的最好方式是什么?谢谢。

4

3 回答 3

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; }
}

正如 Qazi 提到的,您应该具有导航属性。然后在客户端,您应该为您的 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 属性,但需要分配导航属性。

于 2016-04-19T08:46:56.413 回答
0

在使用关系数据库时,我的理解是 studentaddress 将是 Student 的孩子,如代码中 StudentAddress 和 StudentPhoto 表中的 StudentId 外键所示。如果您将导航属性添加到 Student 并将地址和照片分配给这些属性,实体框架将保存它们。就像是

Student s = new Student {...}
s.StudentAddress = new StudentAddress {...}
s.StudentPhoto = new StudentPhoto {...}

db.Students.Add (s);
db.SaveChanges();
于 2014-08-05T22:08:19.437 回答
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");
}
于 2013-09-26T18:49:44.583 回答