Entity Framework does not generates ObservableCollection

c# entity-framework-6 winforms

Question

Im using EF6 in my dataentry program. EF does not generates ObservableCollection but HashSet and ICollection instead , so i have to change it manually. Unfortunately every time i Update Model from Database , every Many-Many relation goes back to ICollection ...

Accepted Answer

Replace ICollection and HashSet with ObservableCollection in your .tt file.
Then search for the method public string UsingDirectives.
In this method there should be a line includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",.
Replace only Generic with ObjectModel. This will include the correct namespace to use ObservableCollections in your models.


Popular Answer

  1. Open the Solution Explorer and find .edmx file
  2. Find the .tt file which will be nested under the .edmx file
  3. Double-click on the XXXModel.tt file to open it in the Visual Studio editor
  4. Find and replace the two occurrences of “ICollection” with “ObservableCollection”. These are located approximately at lines 296 and 484.
  5. Find and replace the first occurrence of “HashSet” with “ObservableCollection”. This occurrence is located approximately at line 50. Do not replace the second occurrence of HashSet found later in the code.
  6. Find and replace the only occurrence of “System.Collections.Generic” with “System.Collections.ObjectModel”. This is located approximately at line 424.
  7. Save the XXXModel.tt file. This should cause the code for entities to be regenerated. If the code does not regenerate automatically, then right click on XXXModel.tt and choose “Run Custom Tool”.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why