Entity Framework + AutoMapper ( Entity to DTO and DTO to Entity )

后端 未结 8 1239
逝去的感伤
逝去的感伤 2021-01-30 07:38

I\'ve got some problems using EF with AutoMapper. =/

for example :

I\'ve got 2 related entities ( Customers and Orders ) and they\'re DTO classes :

         


        
相关标签:
8条回答
  • 2021-01-30 08:18

    AutoMapper is very expressive when it comes to mapping error. read the exception message carefully.

    another important thing is to remember to call Mapper.AssertConfigurationIsValid(); after creating the mappings. it gives an error if the mapping is wrong, thus preventing an exception later in the application runtime.

    0 讨论(0)
  • 2021-01-30 08:20

    Your problem is because Automapper loses the EntityKey associated with the record. As the EntityFramework does not by default handle POCO's (Plain Old CLR Object)

    Jay Zimmerman has a good example here of how to handle this from is. gd /4NIcj Also from Jaroslaw Kowalski (part of the EF team I believe ) has this example for using POCO's within EF, which may translate well to use with Automapper (I've not yet had a chance to try it) : http://blogs.msdn.com/jkowalski/archive/2008/09/09/persistence-ignorance-poco-adapter-for-entity-framework-v1.aspx

    0 讨论(0)
提交回复
热议问题