在 .NET 的 Breeze 中,如何将 DTO 与 Entity Framework 实体一起保存?
In Breeze for .NET, how do you save DTOs alongside Entity Framework entities?
官方documentation中提到DTO可以作为breeze实体使用:
It does not have to be an ORM class. It could be a DTO class that you will later map into a class in your business model via your implementation of BeforeSaveEntities.
在 Ward Bell 的 comment 中,他建议采用以下策略将 DTO 与 EF 实体一起保存:
- Remove DTO from EntityInfos
- Retrieve corresponding business model entity from Db (or create such an entity if this is an insert)
- Update this copy from DTO
- Add this entity to the EntityInfos (don't forget the OriginalValues properties for an update)
- Rinse and repeat for all such DTOs
- Let it go ... and EF will save it
- Intercept the "after save" and remap the updated/inserted business entity into its DTO form in the SaveResult so that you send the DTO, not the "real" entity, back to the client.
此建议的问题在于步骤 4。EntityInfo.Entity 属性 定义为 internal
。如何使用正确的 EF 实体创建 EntityInfo?
此建议的一个替代方法是重写 SaveChangeCore 方法并在该方法中处理 DTO 到 EF 实体的映射。这样做的缺点是 EFContextProvider.SaveChangesCore 有很多代码,我不想重复这些工作。
我找到了 ContextProvider.CreateEntityInfo()。它有一个接受实体并设置 EntityInfo.Entity 属性.
的重载
官方documentation中提到DTO可以作为breeze实体使用:
It does not have to be an ORM class. It could be a DTO class that you will later map into a class in your business model via your implementation of BeforeSaveEntities.
在 Ward Bell 的 comment 中,他建议采用以下策略将 DTO 与 EF 实体一起保存:
- Remove DTO from EntityInfos
- Retrieve corresponding business model entity from Db (or create such an entity if this is an insert)
- Update this copy from DTO
- Add this entity to the EntityInfos (don't forget the OriginalValues properties for an update)
- Rinse and repeat for all such DTOs
- Let it go ... and EF will save it
- Intercept the "after save" and remap the updated/inserted business entity into its DTO form in the SaveResult so that you send the DTO, not the "real" entity, back to the client.
此建议的问题在于步骤 4。EntityInfo.Entity 属性 定义为 internal
。如何使用正确的 EF 实体创建 EntityInfo?
此建议的一个替代方法是重写 SaveChangeCore 方法并在该方法中处理 DTO 到 EF 实体的映射。这样做的缺点是 EFContextProvider.SaveChangesCore 有很多代码,我不想重复这些工作。
我找到了 ContextProvider.CreateEntityInfo()。它有一个接受实体并设置 EntityInfo.Entity 属性.
的重载