实体框架不保存所有更改 [英] Entity Framework does not save all changes

查看:78
本文介绍了实体框架不保存所有更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户对象
这个对象有几个属性FirstName,LastName等所有简单的类型。 String int等。



这些被加载和更新正常。

  public class Jobwalker 
{
[Key]
public Int64 ID {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public string Email {get;组; }
public bool Suspended {get;组; }
public PhoneCountryCode PhoneCountryCodeObject {get;组; }
...

但是,用户对象也有一个属性PhoneCountryCodeObject,这是另一个class

  public class PhoneCountryCode 
{
[Key]
public Guid ID {get;组; }
public string Code {get;组; }
public string国家{get;组; }

public PhoneCountryCode()
{
}
...

我可以创建删除一个dmodify'TelephoneCountryCode'对象只是罚款
,但是当我尝试保存一个用户对象的实例只有简单的类型被更新?

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

db.TelephoneCountryCodes.Attach(TelephoneCountryCodeObject);
Jobwalker result =(来自db.Jobwalkers中的jw
.Include(TelephoneCountryCodeObject)
其中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;
}
}

任何想法都将不胜感激!



编辑:
我创建了这个超简单的测试方法

  public static void TEST()
{
try
{
using(var db = new MainContext())
{
Jobwalker result =(from jw in db .Jobwalkers
.Include(TelephoneCountryCodeObject)
其中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;
}
}

这样做应该是。那么为什么我的常规Save()方法可以工作?

解决方案

如果你需要更新一个分离的实体(例如,您没有从数据库获取实例化对象),您应该具有主键,并使用以下代码:

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

//在你的代码中保存Jobwalker分离对象这样做:
更新(this); //`this`是Jobwalker分离对象
DB.SaveChanges();

不需要额外的代码,EF会检测您的更改并更新实体并添加子实体到数据库。


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.

这篇关于实体框架不保存所有更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆