Theodoros
Theodoros

Reputation: 153

Entity Framework does not generates ObservableCollection

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 ...

Upvotes: 8

Views: 2345

Answers (2)

LightTechnician
LightTechnician

Reputation: 287

  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”.

Upvotes: 5

Domysee
Domysee

Reputation: 12846

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.

Upvotes: 9

Related Questions