English 中文(简体)
B. 实体框架自我跟踪实体的最新情况
原标题:update with entity framework self tracking entities

I m having a problem that I thought was easy to resolve. This is my scenario (Entity framework 4 disconnected entities using self tracking). Let s say I have 2 entities: Users, Orders. From an asp.net page a get from the database 1 user and 1 order.

const int userId = 1;
const int orderId = 1;
var userManager = new UserManager();
var orderManager = new OrderManager();
var user = userManager.GetUser(userId);
var order = channelManager.GetChannel(channelId);

user.Orders.Add(order);

现在,我需要建立一种功能,更新用户的订单。

我写道:

public bool UpdateUser(User user)
{
    context.AttachTo("Users", user);

    var stateMgr = context.ObjectStateManager;
    var stateEntry = stateMgr.GetObjectStateEntry(user);

    for (int i = 0; i < stateEntry.CurrentValues.FieldCount; i++)
    {
            bool isKey = false;

            string name = stateEntry.CurrentValues.GetName(i);

            foreach (var keyPair in stateEntry.EntityKey.EntityKeyValues)
            {
                    if (string.Compare(name, keyPair.Key, true) == 0)
                    {
                            isKey = true;
                            break;
                    }
            }
            if (!isKey)
            {
                    stateEntry.SetModifiedProperty(name);
            }
    } 

    context.ApplyCurrentValues("Users", user);

    return context.SaveChanges() > 0;
}

I don t have any error on this function and debugging everything seems to be ok, but when I check on the database the entity is not updated as expected. I thought update a disconnected entity was something simple but apparently is not. Can someone explaing me the logic between the update the entire graph of disconnected object with EF4? Please if you can I need to undestand the logic and not have a collection of links to look at. I already spent some time looking on internet but I m finding so many approches that I m not sure which one is correct.

增 编

问题回答

自我跟踪 贵实体必须使用“ApplyChanges()”方法,在情况上对变化进行总结(而不是附上)。

申请表将转至图表,以更新相关物体/收集。

 Public Function UpdateEntity(entity As Entity) As Entity

    Using dbContext As New EntityContext()
        dbContext.EntitySet.ApplyChanges(entity)
        dbContext.SaveChanges()

        dbContext.Refresh(Objects.RefreshMode.StoreWins, entity)
    End Using

    Return entity 
End Function

复读是选择性的,在这里只能推回数据库的最后价值。 复读方式没有更新相关物体。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签