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 ...
Asked
Active
Viewed 2,275 times
8
-
There will be two `.tt` files in your project. Open it in a text editor and replace `HashSet` with `ObservableCollection`. – Mat J Dec 24 '13 at 13:38
-
do i have to change both .tt files? – Theodoros Dec 24 '13 at 13:42
-
No, one will be generating the context class, you need to change the other one only which is the one responsible for generating the entity classes. – Mat J Dec 24 '13 at 13:57
2 Answers
9
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.

Domysee
- 12,718
- 10
- 53
- 84
5
- Open the Solution Explorer and find .edmx file
- Find the .tt file which will be nested under the .edmx file
- Double-click on the XXXModel.tt file to open it in the Visual Studio editor
- Find and replace the two occurrences of “ICollection” with “ObservableCollection”. These are located approximately at lines 296 and 484.
- 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.
- Find and replace the only occurrence of “System.Collections.Generic” with “System.Collections.ObjectModel”. This is located approximately at line 424.
- 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”.

LightTechnician
- 277
- 1
- 4
- 14