简体   繁体   English

将实体框架与现有模型一起使用

[英]Using Entity Framework with Existing Models

I am working on a MVC4 project which will need to use a number of different databases, each with a few stored procedures for searching. 我正在研究一个MVC4项目,该项目将需要使用许多不同的数据库,每个数据库都有一些用于搜索的存储过程。 The site is an asset search tool which needs to query various existing systems. 该站点是资产查询工具,需要查询各种现有系统。 If I allow the EF to generate models on its own, I will end up with a Model for each procedure I use in each database. 如果我允许EF自己生成模型,那么我将为在每个数据库中使用的每个过程最终得到一个模型。

What I would prefer is to have my own POCO model already defined and the EF maps its results to that Model. 我希望已经定义了自己的POCO模型,并且EF将其结果映射到该模型。 So regardless of what database the data is taken from it maps back to that same Model. 因此,无论从哪个数据库中获取数据,都将映射回同一模型。 The column names in each database differ slightly so it would really need to be mapping columns to model properties. 每个数据库中的列名略有不同,因此确实需要将列映射到模型属性。

There is no writing back to the database, it purely selects data out. 没有写回数据库的操作,它纯粹是选择数据。

On the 'Edit Function Import' form I can create a model based on the results. 在“编辑函数导入”表单上,我可以基于结果创建模型。 There is also an option to view 'Function Import Mapping' but it does not appear to do what I am looking for. 还有一个选项可以查看“功能导入映射”,但它似乎没有执行我想要的操作。

Has anyone else tried this? 还有其他人尝试过吗?

Added an image to help explain the issue 添加了一张图片以帮助解释问题

描述数据访问

The closest to this I have managed so far is to have EDMX1 query 2 databases. 到目前为止,我管理的最接近的数据库是EDMX1查询2数据库。 This only works because they are on the same Db server. 这仅起作用,因为它们位于同一台Db服务器上。 I had to fully qualify the Db names in the stored procedure. 我必须完全限定存储过程中的Db名称。 I could then use 1 EF Model as a return type for the 2 queries. 然后,我可以将1 EF模型用作2个查询的返回类型。 That Model still is not usable in another EDMX though, so if I need to connect to a different Db server, I still cannot share the Model. 但是,该模型仍然无法在其他EDMX中使用,因此,如果我需要连接到其他Db服务器,则仍然无法共享该模型。 So the problem is not solved. 因此问题没有解决。

Here is image of current progress. 这是当前进度的图像。

到目前为止的进展

Function Import Mapping is for mapping stored procedure / function calls to EF code. 函数导入映射用于将存储过程/函数调用映射到EF代码。 It's not really relevant here, unless you're using stored procs (which is not the way to go 90% of the time with EF - only use stored procs for more complex procedures). 除非您使用存储的proc(在EF中90%的时间都行不通-仅对更复杂的过程使用存储的proc),否则这在这里并不重要。

An EF context, by its very nature, can only have a single database associated with it. 就其本质而言,EF上下文只能有一个与其关联的数据库。 You need to create multiple contexts in order to access multiple databases at once. 您需要创建多个上下文才能一次访问多个数据库。

What I would do in your case is create a database-first schema (.edmx) file for each database, then write a service layer abstraction above it that allows you to flatten the data into your expected model. 在您的情况下,我要做的是为每个数据库创建一个数据库优先的架构(.edmx)文件,然后在其上方编写一个服务层抽象,以使您可以将数据展平为所需的模型。 This is the kind of thing I do all the time, regardless of how many databases I'm working on at once. 不管我一次正在处理多少个数据库,这都是我一直在做的事情。 You've almost outlined this in your first diagram. 您几乎已经在第一个图中概述了这一点。 The service layer may have multiple classes (for example, for a blog website you might have BlogService, UserService, CommentService etc), each of which contain methods that you call from you application layer. 服务层可能具有多个类(例如,对于博客网站,您可能具有BlogService,UserService,CommentService等),每个类都包含您从应用程序层调用的方法。

I've put a quick diagram together that might help to explain 我整理了一个快速图表,可能有助于解释

http://www.gliffy.com/go/publish/image/4818386/L.png http://www.gliffy.com/go/publish/image/4818386/L.png

The service layer does all of your EF work, and your application layer (or business layer, whatever you want to call it) will do all of your business logic. 服务层完成所有EF工作,而应用程序层(或业务层,无论您想称呼它)将完成所有业务逻辑。

This setup lends itself well to TDD and Dependency Injection / IoC. 此设置非常适合TDD和依赖注入/ IoC。 Everything is neat and nicely separated. 一切都整齐而完美地分开了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM