IOC with Entity Framework

纵然是瞬间 提交于 2019-12-06 01:43:31

问题


I am trying to make use of Unity Framework with Entity Framework. Let me explain the scenario. Let's say I have a Database with 5 tables. I will have 5 interfaces each of them corresponding to one table in Database having each field of table as a member. Now I want my Entity Framework generated classes to implement corresponding table interface. All navigational properties should return objects as interface references. This should allow me to resolve these entities using Unity framework to allow any one to extend EF(data) entities without breaking required codes. Is this possible?


回答1:


No there is no extensibility point for resolving entities with IoC container. EF will always handle instance creation of persisted entities. Morover EF mapping must work with classes not interfaces.

You can only handle ObjectMaterialized event on ObjectContext and provide some additional initialization of created entities.

You can try to add interfaces by implementing custom T4 template and including some convention (like name of the interface is always I + name of the entity) but still without IoC.



来源:https://stackoverflow.com/questions/5279372/ioc-with-entity-framework

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