Serializable classes and dynamic proxies in EF - how?

前端 未结 5 464
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 12:08

In [a previous posting], I was set on the path to having to clone my entities. This I\'ve attempted to do with a serialisation approach as found in [codeproject].

b

5条回答
  •  攒了一身酷
    2020-11-29 12:49

    As a Microsoft recommendation, you need to use DTO instead of EF entities to avoid serialization issues, consider also to use eager loading before transforming your entities to DTO.

    One way to avoid serialization problems is to serialize data transfer objects (DTOs) instead of entity objects.

    ...

    What happens if you add the corresponding navigation property [...]?

    Unfortunately, this creates a problem when you serialize the models. If you load the related data, it creates a circular object graph.

    One solution is to use DTOs, [...] Alternatively, you can configure the JSON and XML formatters to handle graph cycles.

    EF Documentation - How to create a Web API application that uses Entity Framework for database persistence.

提交回复
热议问题