2016-08-02 28 views
1

我有一個複合主鍵模型多對多關係的實體:如何取消刪除實體EntityState刪除

public class ActualAnswer 
{ 
    [Key] 
    [Column(Order = 0)] 
    public int AnsweredQuestionID { get; set; } 

    [Key] 
    [Column(Order = 1)] 
    public int AnswerID { get; set; } 

} 

如果我刪除使用Breeze在我的客戶端應用程序,這些實體之一,它的狀態設置爲已刪除:

function deleteEntity(entity) { 
    var ea = entity.entityAspect; 
    ea.setDeleted(); 
} 

如果用戶改變他們的想法我現在嘗試重新建立實體:

createEntity("ActualAnswer", { 
    AnsweredQuestionID: answeredquestionid, 
    AnswerID: answerid 
}); 

使用我的微風EntityManager調用該函數:

function createEntity(entityType, initialValues) { 
    var entity = manager.createEntity(entityType, initialValues) 
    return entity; 
} 

但是,會導致一個錯誤:

A MergeStrategy of 'Disallowed' does not allow you to attach an entity when an entity with the same key is already attached: ActualAnswer:#etc

這是真的,我們已經有一個具有相同密鑰的實體 - 但它是在一個「已刪除」狀態。

那麼,我該如何檢索並取消刪除它?

或者,我可以安全地使用不同的合併策略嗎?我需要注意哪些缺陷?我是否可以通過僅對此特定實體使用合併策略來降低風險?

回答

1

這是我能找到的檢索從緩存中刪除項目以及取消它們的唯一方法:

//A MergeStrategy of 'Disallowed' does not allow you to attach an entity 
//when an entity with the same key is already attached 
//so we need to check for deleted entities and undelete them 
var queryOptions = manager.queryOptions.using({ 
    includeDeleted: true, 
    fetchStrategy: breeze.FetchStrategy.FromLocalCache 
}); 

var existing = EntityQuery 
    .from('ActualAnswers') 
    .where('AnsweredQuestionID', '==', this.ID) 
    .where('AnswerID', '==', answerid) 
    .using(queryOptions) 
    .using(manager) 
    .executeLocally(); 

if (existing.length > 0 && existing[0].entityAspect.entityState.isDeleted()) { 
    //rejectChanges fixes navigation properties. setUnchanged doesn't 
    existing[0].entityAspect.rejectChanges(); 
} 
else { 
    createEntity("ActualAnswer", { 
     AnsweredQuestionID: this.ID, 
     AnswerID: answerid 
    }); 
} 
0

你爲什麼不只是改變狀態,以修改呢?

function unDeleteEntity(entity) { 
    var ea = entity.entityAspect; 
    ea.setModified(); 
} 
+0

該實體模擬多對多的關係。如果用戶刪除關係,那麼它應該被標記爲在服務器上刪除。如果用戶重新創建關係,則就服務器而言,它處於不變的狀態。沒有什麼可修改的這並沒有告訴我如何找回被刪除的實體來取消刪除它 – Colin