2012-12-29 15 views

回答

1

你可以試試下面的一個。

插入斷開實體

這裏是Insert<>通用版本,它可以插入任何斷開連接的實體。

public TEntity Insert<TEntity>(TEntity entity) 
      where TEntity : EntityObject 
{ 
    AddTo<TEntity>(entity); 
    this.SaveChanges(true); 
    // Without this, attaching new entity of same type in same context fails. 
    this.Detach(entity); 
    return entity; 
} 

插入斷開子實體

插入子實體的共同原理是:

  1. 首先你得父實體連接的背景下,

  2. 然後你將不得不設置父母和孩子之間的映射(你不能有mappi ng已經!),

  3. 然後你將不得不打電話SaveChanges。

下面的代碼:

您可以從Entity Framework working in fully disconnected Here

瞭解詳細內容,我希望這會幫助你。

相關問題