实体框架4 - 实现针对实体的接口 [英] Entity Framework 4 - Implementing Interfaces against Entities

查看:74
本文介绍了实体框架4 - 实现针对实体的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在搞乱EF,并遇到了我希望模型中的实体实现接口的场景。实际上,我正在开发一个具有核心数据库的系统,该数据库连接到一个充满受限视图的数据库(可能不是最好的实现,但我目前无法改变这一点)。



我有效地希望为整个数据库创建接口,以便我可以互换地使用普通和受限制的数据库。



我添加了一个EntityObject生成器并更改了模板,以便生成的代码引用接口而不是具体项,但是当我尝试运行测试时,我收到一条错误,指出映射和元数​​据信息可以'找到EntityType。



我感觉EF模型不支持继承,而且它只是底层数据的具体实现。



任何人都可以确认这是真的还是给我一些指示我最好如何处理这个?



提前致谢

I'm currently messing around with EF and have come across a scenario where I want the entities in my model to implement interfaces. Effectively I'm working on a system that has a core database, connected to which is a database full of restricted views (possibly not the best implementation but there's no way I can currently get this changed).

I effectively want to create interfaces for the entire database so that I can use the normal and restricted databases interchangably.

I've added an EntityObject generator and changed the template so that the code that is produced references interfaces rather than concrete items, but when I try to run a test, I get an error stating that mapping and metadata information can't be found for the EntityType.

I've got a feeling that the EF model doesn't support inheritance and that it's only a concrete implementation of the underlying data.

Can anyone confirm if this is true or give me some indication of how I could best proceed with this?

Thanks in advance

推荐答案

你所说的是Persistence Ignorance,它是EF支持的一项功能。实际上,您希望将实体转换为POCO。你应该阅读这个 [ ^ ]有关如何执行此操作的详细信息。
What you are talking about is Persistence Ignorance, and is a feature that is supported in EF. Effectively, you want to convert the entity into a POCO. You should read this[^] for details on how to do this.


Alternatively, you can download my project on Codeplex. I have a working example that you can copy.
https://entityinterfacegenerator.codeplex.com/

It generates the interface files that you need for IoC purposes.


这篇关于实体框架4 - 实现针对实体的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆