Entity Framework does not generates ObservableCollection

ε祈祈猫儿з 提交于 2019-12-12 07:37:17

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/20761622/entity-framework-does-not-generates-observablecollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!