Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update one field of specific records using Entity Framework?

I want update family of a person who his name is pejman. This is my object Class:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set;}
    public DateTime BirthDate { get; set; }
    public bool IsMale { get; set; }
    public byte[] Image { get; set; }
    public byte[] RowVersion { get; set; }
    public virtual Person Parent { get; set; }
    public virtual ICollection<PhoneNumber> PhoneNumber { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual PersonInfo PersonInfo { get; set; }
}

and my method for updating is: (in Program.cs)

public static void Update(string name, string family)
{
    var _person = new Person() { FirstName = name, LastName = family };

    using (var newContext = new MyDbContext())
    {
        newContext.Persons.Attach(_person);
        newContext.Entry(_person).Property(X => X.LastName).IsModified = true;
        newContext.SaveChanges();
    }
}

but it doesn't work! what is the problem?

EDIT: assume that i don't know person's Id, and i just know person's name, is there any way to update person's family?

like image 347
pejman Avatar asked Jan 25 '14 12:01

pejman


People also ask

How do you update database changes in Entity Framework first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish. After the update process is finished, the database diagram includes the new MiddleName property.

What is EntityState modified?

EntityState.Added : EntityState.Modified; context.SaveChanges(); } } Note that when you change the state to Modified all the properties of the entity will be marked as modified and all the property values will be sent to the database when SaveChanges is called.


2 Answers

You are missing an Id field when creating an instance of Person object. Because of this Entity Framework is not able to find an existing Person.

Your code should look like this:

public static void Update(int id, string name, string family)
{
    var _person = new Person() { Id = id , FirstName = name, LastName = family };

    using (var newContext = new MyDbContext())
    {
        newContext.Persons.Attach(_person);
        newContext.Entry(_person).Property(X => X.LastName).IsModified = true;
        newContext.SaveChanges();
    }
like image 164
Paweł Bejger Avatar answered Oct 11 '22 12:10

Paweł Bejger


EDIT: assume that i don't know person's Id, and i just know person's name, is there any way to update person's family?

I'm assuming that the FirstName field of Person class contains the person's name, and updating the person's family means updating the LastName field.

The first step is to get all Person records with FirstName equals the person's name, let's say it's "pejman" so the code would be as below.

var personsToUpdate = newContext.Persons.Where(o => o.FirstName == "pejman");

The next step is enumerate personsToUpdate and set the LastName property to the family name you want, then call .SaveChanges() method of the DbContext to submit the changes to the database. Let's say you want to update the person's family name to "MyFamilyName", the code would be as below

foreach (Person p in personsToUpdate)
{
    p.LastName = "MyFamilyName";
}

newContext.SaveChanges();

The following is the modified code of your Update method with the name and family parameter.

public static void Update(string name, string family)
{
    using (var newContext = new MyDbContext())
    {
        // get all Persons with FirstName equals name
        var personsToUpdate = newContext.Persons.Where(o => o.FirstName == name);

        // update LastName for all Persons in personsToUpdate
        foreach (Person p in personsToUpdate)
        {
            p.LastName = family;
        }

        newContext.SaveChanges();
    }
}
like image 25
ekad Avatar answered Oct 11 '22 10:10

ekad