Entity Framework POCO objects

2020-07-23 06:02发布

问题:

I'm struggling with understanding Entity Framework and POCO objects. Here's what I'm trying to achieve.

1) Separate the DAL from the Business Layer by having my business layer use an interface to my DAL. Maybe use Unity to create my context.

2) Use Entity Framework inside my DAL.

I have a Domain model with objects that reside in my business layer. I also have a database full of tables that doesn't really represent my domain model. I setup Entity Framework and generated POCO objects by using the ADO.NET POCO Generator extension. This gave me an object for each table in my database. Now I want to be able to say context.GetAll<User>(); and have it return a list of my User objects. The User object is in my business layer. Is that possible?

Does that make sense or am I totally off and should start over? I'm guessing I need to use the repository pattern to achieve this, but I'm not sure.

Can anyone help?

回答1:

Examples how to put entity framework 4 in n-tier arhitecture in order of complexity:

  1. http://devtalk.dk/2009/06/09/Entity+Framework+40+Beta+1+POCO+ObjectSet+Repository+And+UnitOfWork.aspx
  2. http://blog.keithpatton.com/2009/05/30/Entity+Framework+POCO+Repository+Using+Visual+Studio+2010+Net+40+Beta+1.aspx
  3. http://danielwertheim.files.wordpress.com/2009/12/putting-entity-framework-4-to-use-in-a-business-architecture-v2.pdf
  4. http://www.simonsegal.net/blog/2010/01/11/entity-framework-repositories-fetching-strategies-specification-and-mapping-using-nfetchspec-for-role-driven-development-parts-1-4


回答2:

In general, with EF, the way to get all of the objects in your context as a List<T> would be something like:

context.Users.ToList();

But since Users would be an IEnumerable in your context anyway, you can do virtually any kind of iteration or querying directly on context.Users