0

I have two entities (student and studentImage). When updating info for student, I get the student and modify data in it. If I've modified data from both the student and studentImage why is it that when I go to my repository to update I only have to run the second line here shown below? And it updates both the student and studentImage rows. you would think I would have to update both or at least the student, but if I try to run both lines of code I get an exception thrown...It seems like when I update student.StudentImage it progagates upward to student?

public void Update(Student student)
    {
        //context.Entry(student).State = EntityState.Modified;
        context.Entry(student.StudentImage).State = EntityState.Modified;
    }
public void Save()
    {
        context.SaveChanges();
    }

Here are my student and studentImage entities jsut for show.

public class StudentImage
{
    [Key, ForeignKey("Student")]
    public int StudentId { get; set; }

    public byte[] Image { get; set; }

    public byte[] ImageThumbnail { get; set; }

    public string ContentType { get; set; }

    public virtual Student Student { get; set; } // one-to-one
}

public class Student
{
    public int StudentId { get; set; }

    public virtual StudentImage StudentImage { get; set; } //one-to-one

    [Required]
    public DbGeography LocationPoints { get; set; } //37.1234, -122.2342

    [Required]
    public string Location { get; set; } //ex. San Francisco, CA, USA

    [MaxLength(250)]
    public string Education { get; set; } //State Univesity

    public string Work { get; set; } // Taco Bell, Starbucks

    public StudentStatus Status { get; set; }
}
chuckd
  • 13,460
  • 29
  • 152
  • 331
  • Because the StudentImage has a 1-1 reference t the student. – Benjamin Gruenbaum Sep 20 '15 at 20:16
  • Are you asking about *why*, conceptually, changing `student.StudentImage` *should* force an update of both tables? Or are you wondering *how* the technology detects this fact and causes it to happen? – StriplingWarrior Sep 20 '15 at 20:26
  • Also comment out the second line and you'll be surprised. There's no need at all to mark the entities as modified. You stay in the scope of one context, so they already *are* modified. – Gert Arnold Sep 20 '15 at 20:32
  • I just want to make sure I'm doing things correctly, it just doesn't seem correct to make a change to student and then update studentImage. – chuckd Sep 20 '15 at 20:33
  • what happens if I create another entity off of student? Call it studentAddress then how do I update student when I change a value? Do I update studentAddress or studentImage? or either one? – chuckd Sep 20 '15 at 20:34
  • Did you understand what I said? The way you work, i.e. not attaching entities to a new context, you don't need this `Update` method. EF tracks the changes and updates wherever necessary. – Gert Arnold Sep 20 '15 at 20:38
  • 1
    If you want an in-depth treatise on this, go here: http://stackoverflow.com/q/21758807/861716 – Gert Arnold Sep 20 '15 at 21:11
  • So what's the point of the update/modified statement from ef if I don't need them? it was created when I added a controller and it asked me if I want save/update/deletes created Also you aren't correct to say that I can remove the second line, when I do and I have modified a studentImage an exception gets thrown! – chuckd Sep 20 '15 at 21:40
  • here is the error I get if I comment out the update statement "{"Violation of PRIMARY KEY constraint 'PK_dbo.StudentImages'. Cannot insert duplicate key in object 'dbo.StudentImages'. The duplicate key value is (5).\r\nThe statement has been terminated."}" – chuckd Sep 20 '15 at 21:49
  • it's trying to insert a new record if I don't run the modified statement update and I try to update the picture – chuckd Sep 20 '15 at 21:49
  • Scaffolding is aimed at generating *working* code, not necessarily the *best* code. You seem to have attached `student` before marking it as modified, that led me to think that you were in a connected scenario. Your question lacks details about what happens before you call `Update`. – Gert Arnold Sep 20 '15 at 22:42
  • My bad! I was trying to create a new studentImage and attach it to the student entity, I wasn't modifying the studentImage that was part of the student entity. All is good now! Thanks for the help! – chuckd Sep 20 '15 at 22:46

1 Answers1

1

Entity Framework does a pretty good job at determining which records need to be updated when you Save. It also handles change tracking automatically - you don't even need to mark your entity as modified due to the way it uses proxies.

If you Save an entity, it'll look at any associations that the entity depends on and check those for changes also. After it has determined which records need to be updated/added, it will generate the query that updates these records in a way that will satisfy constraints.

A.Konzel
  • 1,920
  • 1
  • 13
  • 14