简体   繁体   中英

Entity Framework does not save all changes

Im very close to tears on this one.

I have a user-object this object has several properties FirstName, LastName etc. all simple types. String int etc.

These get loaded and updated fine.

public class Jobwalker
{
    [Key]
    public Int64 ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public bool Suspended { get; set; }
    public TelephoneCountryCode TelephoneCountryCodeObject { get; set; }
    ...

But the userobject also has a property 'TelephoneCountryCodeObject' which is another class

public class TelephoneCountryCode
{
    [Key]
    public Guid ID { get; set; }
    public string Code { get; set; }
    public string Country { get; set; }

    public TelephoneCountryCode()
    {
    }
    ...

I can create delete an dmodify 'TelephoneCountryCode'-objects just fine, but when i try to save an instance of a user-object only the simple types gets updated?

public void Save()
    {
        try
        {
            using (var db = new MainContext())
            {

                db.TelephoneCountryCodes.Attach(TelephoneCountryCodeObject);
                Jobwalker result = (from jw in db.Jobwalkers
                                        .Include("TelephoneCountryCodeObject")
                                    where jw.UmbracoMemberID == UmbracoMemberID
                                    select jw).FirstOrDefault();


                db.Entry(result).CurrentValues.SetValues(this);

                if (this.TelephoneCountryCodeObject != null)
                {
                    result.TelephoneCountryCodeObject = db.TelephoneCountryCodes.Find(this.TelephoneCountryCodeObject.ID);
                }
                else
                {
                    result.TelephoneCountryCodeObject = null;
                }

                db.Entry(result).State = EntityState.Modified;

                db.SaveChanges();
            }
        }
        catch(Exception ex)
        {
            throw;
        }
    }

Any ideas would be greatly appreciated!

EDIT: I created this super simple test method

 public static void TEST()
    {
        try
        {
            using (var db = new MainContext())
            {
                Jobwalker result = (from jw in db.Jobwalkers
                                        .Include("TelephoneCountryCodeObject")
                                    where jw.UmbracoMemberID == 1184
                                    select jw).FirstOrDefault();



                result.TelephoneCountryCodeObject = TelephoneCountryCode.GetByName("denmark");
                db.TelephoneCountryCodes.Attach(result.TelephoneCountryCodeObject);
                result.FirstName = "Flemming";

                db.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }

This works like it is supposed to. Then why the * * wont my regular Save() method work?

If you need to update an entity which is detached (for example you instantiated the object without fetching from database), you should have it's primary key and use the code below:

public void Update(Jobwalker detachedEntity)
{
    DB.Jobwalkers.Attach(detachedEntity);
    DB.Entry(detachedEntity).State = EntityState.Modified;
}

// in your code to save Jobwalker detached object do this:
Update(this); // `this` is the Jobwalker detached object
DB.SaveChanges();

There is no need for extra code, EF will detect your changes and update the entity and add the child entities to database.

This is more than likely a result of TelephoneCountryCodeObject not being a dynamic proxy from the database, and probably just being a plain object. I believe that if you attach the result, then you will be able to save the changes.

Jobwalker result = (from jw in db.Jobwalkers
  .Include("TelephoneCountryCodeObject")
  where jw.UmbracoMemberID == UmbracoMemberID
  select jw).FirstOrDefault();
db.TelephoneCountryCodes.Attach(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM