How to disable automapping of properties in Entity Framework

扶醉桌前 提交于 2019-12-02 01:12:56

问题


I've decided to use fluent mapping in Entity Framework. My intention was to map everyting by code without any atributes and auto mapping functions. Best way I've found is class EntityTypeConfiguration, that I implement for each entity in my project.

Later I add property to one of my entity. This property isn't needed to be persisted. I've expected, that until I add mapping for this property, it will be ignored by database and persistence layer. Unfortunatly it doesn't work that way, and property is mapped. Only way is to use Ignore method or NotMapped attribute, but I don't want to do it explicitly.

Is there any way, to stop Entity Framework from automapping? I've tried to remove all Conventions from DbModelBuilder, but it doesn't help.


回答1:


So far as I am aware, there is no other way around it. You need to use either Ignore() or [NotMapped]. I tend to prefer the former as it does not clutter up the model.




回答2:


Actually I have tried a lot of ways: - custom convention to remove mapped properties - removing all conventions

But the easiest (and cleanest) way was to use reflection inside the mapping class and to disable all property mappings that weren't configured.

The code for that (and also an usage example) is inside my public gist. https://gist.github.com/hidegh/36d92380c720804dee043fde8a863ecb



来源:https://stackoverflow.com/questions/13607591/how-to-disable-automapping-of-properties-in-entity-framework

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