实体框架更新上的POCO SaveChanges()不起作用? [英] Entity Framework POCO SaveChanges() on Update doesn't work?

查看:320
本文介绍了实体框架更新上的POCO SaveChanges()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF CTP 4,使用POCO模型,添加一个新对象并调用SaveChanges(),但更新对象不起作用。以下是更新代码:

  public void UpdateContact(Contact contact)
{
var findContact = GetContact (contact.ContactID);
findContact = contact;
_context.SaveChanges();
}

public Contact GetContact(int contactId)
{
返回GetAllContacts()。SingleOrDefault(c => c.ContactID == contactId);
}

public IQueryable< Contact> GetAllContacts()
{
return _context.Contacts;
}

我不知道我在这里做错了什么。任何想法?谢谢。

解决方案

问题是当您分配 findContact = contact EntityState ObjectStateManager 中不会更改(因此仍然设置为不变)。因此,不为实体生成更新 SQL语句。您有几个选项来进行更新:



选项1:填充 findContact 字段-by-field:

  var findContact = GetContact(contact.ContactID); 
findContact.FirstName = contact.FirstName;
findContact.LastName = contact.LastName;
...
_context.SaveChanges();

选项2:使用 ApplyCurrentValues 方法:

  var findContact = GetContact(contact.ContactID); 
_context.ApplyCurrentValues(Contacts,contact);
_context.SaveChanges();

选项3:附加更新的实体并更改 ObjectStateManager 。 (注意:这不会使数据库往返于现有的联系人)。

  _context.AttachTo(Contacts , 联系); 
var contactEntry = Context.ObjectStateManager.GetObjectStateEntry(contact);
contactEntry.ChangeState(EntityState.Modified);
_context.SaveChanges();


I'm working with EF CTP 4, using POCO models, adding a new object and call SaveChanges() works but updating the object doesn't work. Here's the code for update:

public void UpdateContact(Contact contact)
        {
            var findContact = GetContact(contact.ContactID);
            findContact = contact;
            _context.SaveChanges();
        }

public Contact GetContact(int contactId)
        {
            return GetAllContacts().SingleOrDefault(c => c.ContactID == contactId);
        }

public IQueryable<Contact> GetAllContacts()
        {
            return _context.Contacts;
        }

I'm not sure what I'm doing wrong here. Any idea? Thanks.

解决方案

The problem is that when you assign findContact = contact the EntityState does not get changed in the ObjectStateManager (so it's still set to Unchanged). Therefore, no Update SQL statement gets generated for the entity. You have several options to do the update:

Option 1: Populate findContact field-by-field:

var findContact = GetContact(contact.ContactID);
findContact.FirstName = contact.FirstName;
findContact.LastName = contact.LastName;
...
_context.SaveChanges();

Option 2: Use the ApplyCurrentValues method:

var findContact = GetContact(contact.ContactID);
_context.ApplyCurrentValues("Contacts", contact);
_context.SaveChanges();

Option 3: Attach the updated entity and change the state in the ObjectStateManager. (Note: this will not make a roundtrip to the database for fetching the existing contact).

_context.AttachTo("Contacts", contact);
var contactEntry = Context.ObjectStateManager.GetObjectStateEntry(contact);
contactEntry.ChangeState(EntityState.Modified);
_context.SaveChanges();

这篇关于实体框架更新上的POCO SaveChanges()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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