Custom Data Access in EF for POCO

不问归期 提交于 2019-12-08 07:50:50

问题


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.


回答1:


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 :).



来源:https://stackoverflow.com/questions/8172123/custom-data-access-in-ef-for-poco

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