此問題源於此其他article。我目前在實體框架中使用BreezeJS,但我不相信我會按照預期的方式使用BreezeJS。目前我正在調用保存對我發送到服務器的實體數組的保存更改,然後將它們反序列化爲它們的原始結構。我希望有一個更好的方法來做到這一點,我一直沒有找到。有兩種方式我看到我可以做到這一點,但無法讓他們工作。BreezeJS saveChanges發送數組而不是對象
首先將數組作爲單個對象發送,當反序列化時已經存在於對象結構中。當客戶端是對象被保存的格式時,它不會需要任何額外的工作。
第二種選擇是以某種方式使用發送到服務器的數組,並使用EFContextProvider中的實體框架元數據構建對象結構。
如果可能,我寧願選擇更接近選項一。
的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對象中的一個子對象。
// 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錯誤但確認和數據庫保存在單獨的模塊發生進一步的路線。我只是試圖刪除一些中間的手動工作(不得不重建對象結構)。
即使實體與客戶端相關,Breeze也會將它們拆分爲數組以避免在序列化爲JSON時出現循環引用問題。在服務器上,EFContextProvider將它們添加到EF上下文; EF在此過程中自動重建對象圖。 –
這就是問題所在,我找不到任何這樣做的例子。在服務器上,我有發送的實體數組,但我無法弄清楚如何使用我的EFContextProvider從數組中構建我的對象。 –
但爲什麼你需要這樣做,當它自動爲你做? –