EF4 and undesired loading of related collection with AddObject

前端 未结 1 523
走了就别回头了
走了就别回头了 2020-12-09 22:20

I have an odd case where adding a record is causing unwanted loading of a related collection.

For example, I have Requests and Sessions. A Session can contain many

相关标签:
1条回答
  • 2020-12-09 23:18

    I guess you are using POCO T4 template which suspects exactly this behavior. The problem are fixup methods generated by POCO template. Every time you assign navigation property, foreign key or add object to collection of related objects these methods performs object graph fixup. It means they update navigation on related entity as well. In your scenario it means that fixup methods adds Request to Requests collection in the Session. Access to the collection will trigger lazy loading. The only ways to avoid this are:

    • Turn off lazy loading for this operation (context.ContextOptions.LazyLoadingEnabled = false)
    • Remove Requests property from Session
    • Modify T4 template and remove fixup methods
    • Modify T4 template and remove virtual from Requests property (Session will not support lazy loading)
    0 讨论(0)
提交回复
热议问题