用于POCO的EF中的自定义数据访问 [英] Custom Data Access in EF for POCO

查看:135
本文介绍了用于POCO的EF中的自定义数据访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个WCF数据服务,旨在从预定义的标准结构中的任何数据库提取数据。



我在想使用POCO实体。我可以在EF设计器上设计我的实体,并从中生成POCO类,但是我很难理解的是如何编写数据访问层并将其注入到DBContext中。



因此,对于每个不同的数据库,我将有一个数据访问层,它将从数据库或甚至xml文件中检索数据,并将数据映射到我的POCO实体。



我不知道这是否可以实现。



POCO类将是我的标准结构,以暴露给世界。
我无法看到任何地方编写自定义sql查询从数据库中提取数据,然后在POCO类中设置数据。 POCO类不匹配任何数据库表,所以我明确地需要将数据库字段映射到POCO类,但我不知道如何使用POCO在Entity Framework中执行此操作。



我相信POCO是写入选项,但是在数据访问层和数据库映射到POCO类之间挣扎。



我看到的所有示例都谈到连接EF直接到现有的数据库。意思是EF结构必须匹配数据库的结构。我想要的是一个单一的EF / POCO结构,可以从多个数据库检索数据。这些数据库不具有相同的结构,但是我需要从这些数据库中手动检索数据,并将转换为到POCO类结构中。我不一定要一次从多个数据库获取数据,而是从单个数据库获取数据,但是要为任何数据库使用相同的模型 - 所以我想我必须为每个数据库编写一个自定义DAL,该数据库从数据库获取数据,将数据转换为POCO模型结构。



如果有人能帮助我,我真的很感激。



顺便说一下,我是EF的新手,请耐心等待。

解决方案

你是否遵循这个教程,向您展示如何从数据库中创建模型类并对其进行查询?



另外这里是一个伟大的教程,使用EF代码 - 首先,您首先构建您的POCO类,并为您生成数据库。好阅读:)。


I am currently writing a WCF data service that is meant to extract data from any database in a predefined standard structure.

I was thinking of using POCO entities. I can design my entities on EF designer and generate the POCO classes from it but the bit I struggling to understand is how to write data access layer and inject it into the DBContext.

So for each different database, I'll have a data access layer that will retrieve data from a database or even an xml file and map the data to my POCO entities.

I am not sure if this is achievable at all.

The POCO classes will be my standard structure to expose to the world. I can't see anywhere to write custom sql queries to extract data from a DB and then set the data in the POCO classes. The POCO classes do not match any of the database tables so I explicitly need to map database fields to the POCO classes but I am not sure how to do this in Entity Framework using POCO.

I believe POCO is the write option but struggling on the data access layer and mappings from database to POCO classes.

All the samples I have seen talk about connecting the EF to an existing database directly. Meaning the EF structure has to match the structure of the database. What I want is a single EF/POCO structure that can retrieve data from multiple databases. These databases do not have the same structure but I need to manually retrieve data from these databases and transform it into the POCO classes structures. I do not necessarily want to get data from multiple databases at once but from a single DB but want to use the same model for any database - So I guess I have to write a custom DAL for each database which gets the data from a DB and transform the data into the POCO model structure.

I would really appreciate if anyone could help me.

By the way I am new to EF so please be patient.

解决方案

Have you followed this tutorial which shows you how to create model classes from a DB and query against them?

Also here is a great tutorial on using EF code-first, in which you build your POCO classes first, and it generates the DB for you. Great read :).

这篇关于用于POCO的EF中的自定义数据访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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