BreezeJS saveChanges发送一个数组而不是一个对象 [英] BreezeJS saveChanges sends an array instead of an object

查看:119
本文介绍了BreezeJS saveChanges发送一个数组而不是一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题源于此文章。我目前正在使用BreezeJS与实体框架,但我不相信我正在使用它的方式它的意图。目前我正在调用保存对我发送到服务器的一组实体的保存更改,然后将它们反序列化为其原始结构。我希望有一个更好的方法来做到这一点,我根本找不到。有两种方式可以看到我可以做到这一点,但是无法让它们工作。

This question stems off of this other article. I'm currently using BreezeJS with Entity Framework but I don't believe I am using it quite the way it was intended. Currently I am calling save changes on an array of entities that I am sending to the server and then deserializing them into their original structure. I'm hoping there's a better way to do this that I just haven't been able to find. There are two ways I see I could do this but can't get them to work.

首先将数组发送为单个对象,当反序列化时已经在对象结构中。当客户端是对象被保留的格式时,它不会再需要任何额外的工作。

First would be to send the array over as a single object that when deserialized would already be in the object structure. When client side that is the format the object is held in so it wouldn't take any additional work.

第二个选项是以某种方式使用发送的数组使用EFContextProvider中的实体框架元数据构建对象结构。

The second option would be to somehow use the array that is sent to the server and build the object structure using the Entity Framework Metadata within EFContextProvider.

如果可能,我更愿意使用更接近选项一的解决方案。

If possible I would prefer a solution closer to option one.

JavaScript

Javascript

function saveObjects() {
    // Assume the child class has a foreign key to the parent
    var parent = dataService.createEntity('PARENT', parentObject);
    var child = dataService.createEntity('CHILD', childObject);

    // Save changes
    // This is what I'm currently doing because each entity is seperate
    dataService.saveChanges([parent, child]);
    // This is what I would like to do
    // dataService.saveChanges(parent);
}

当前发送的对象看起来像这样。我想要CHILD实际上是在PARENT对象中发送的孩子。

The object that is currently sent looks like this. I want CHILD to actually be a child within the PARENT object when it gets sent across.

// Current saveBundle
{"entities":  [
    {"ID: 1, 
    "entityAspect": {"entityTypeName": "PARENT", ...}},
    {"PARENT_ID: 1, 
    "entityAspect": {"entityTypeName": "CHILD", ...}}
]}

// Ideal saveBundle
{"entities":  [
    {"ID: 1, 
        {"PARENT_ID: 1, 
        "entityAspect": {"entityTypeName": "CHILD", ...}},
    "entityAspect": {"entityTypeName": "PARENT", ...}},

]}

C#

[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
    // Currently I have to deserialize each object and rebuild the object
    // because the save bundle is a list of single entities instead of
    // the existing object hierarchy
    PARENT parent = DeserializeEntity(saveBundle, 'PARENT');
    parent.child = DeserializeEntity(saveBundle, 'CHILD');

    // Custom Validation and Saving is done here
}

我可能使用BreezeJS不正确,但验证和数据库保存发生在单独的模块进一步下行。我只想在中间删除一些手工工作(不得不重建对象结构)。

I may be using BreezeJS incorrectly but the validation and database saving happens in separate modules further down the line. I'm just trying to cut out some the manual work in the middle (having to rebuild the object structure).

推荐答案

一个解决您的问题的方法是在 BeforeSaveEntities 方法中执行工作。在那里,这些实体已经被反序列化,但是还没有在一个对象图中。您可以通过将EF附加到单独的上下文(与保存相同的上下文)中将EF排列在对象图中。

One way to address your problem is to perform work in the BeforeSaveEntities method. There, the entities have already been deserialized, but are still not in an object graph. You can have EF arrange them in an object graph for you by attaching them to a separate Context (not the same one used for saving).

请参阅这个问题和答案了解更多详情。

这篇关于BreezeJS saveChanges发送一个数组而不是一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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