Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 4.1, Generic Repository and ObservableCollection
    text
    copied!<p>I'm implementing the repository pattern with EF 4.1, <a href="http://huyrua.wordpress.com/2011/04/13/entity-framework-4-poco-repository-and-specification-pattern-upgraded-to-ef-4-1" rel="nofollow">http://huyrua.wordpress.com/2011/04/13/entity-framework-4-poco-repository-and-specification-pattern-upgraded-to-ef-4-1</a></p> <p>My proble is that I'm working in a legacy Winform application and in order to databind the returned collection to grid controls and detect new items, I need an ObservableCollection, but all repository method I have seen on varios repository patterns examples only return IList collections.</p> <p>What I do now is:</p> <pre><code>IList&lt;AccountType&gt; accountTypes = _repository.GetAll&lt;AccountType&gt;().ToList(); var a1 = new AccountType { Name = "Bank2" }; var a2 = new AccountType { Name = "Cash2" }; accountTypes.Add(a1); accountTypes.Add(a2); _repository.UnitOfWork.SaveChanges(); Console.Write("AccountType Saved."); </code></pre> <p>but with this code, added items are not persisted by the repository.</p> <p>Have someone any idea how to avoid this and return a BindigList or ObservableCollection from a generic repository using EF 4.1?</p> <p><strong>EDITED:</strong></p> <p>If I convert the IList returned to a ObservableColletion, do you mean somethink like this test code I have wrote is OK?:</p> <pre><code>[TestMethod] public void CreateAccountList() { _accountTypes = new ObservableCollection&lt;AccountType&gt;(_repository.GetAll&lt;AccountType&gt;().ToList()); _accountTypes.CollectionChanged += CollectionChanged; var a1 = new AccountType { Name = "Bank2" }; var a2 = new AccountType { Name = "Cash2" }; _accountTypes.Add(a1); _accountTypes.Add(a2); _accountTypes.Remove(a2); _repository.UnitOfWork.SaveChanges(); int count = _repository.Count&lt;AccountType&gt;(); Assert.AreEqual(1, count); } void CollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) { switch (notifyCollectionChangedEventArgs.Action) { case NotifyCollectionChangedAction.Add: foreach (var accountType in notifyCollectionChangedEventArgs.NewItems) { _repository.Add((AccountType)accountType); } break; case NotifyCollectionChangedAction.Remove: foreach (var accountType in notifyCollectionChangedEventArgs.OldItems) { _repository.Delete((AccountType)accountType); } break; } } </code></pre> <p>or is there an generic way of doing it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload