EF 5 Layered Architecture RRS feed

  • Question

  • Hello 

    I'm trying to learn MVC 4 and EF 5 by building a sample application where I have my presentation, business logic and repositories modules split by 3 projects and a database (database first approach i suppose).

    I usually generate the model from the database on my repository module since is that on that module that I will interact with the backend, but on the process it generates my entities classes on that module forcing me to make a reference from the presentation layer to the repository or creating an abstraction on business logic, either one of them i don't like it.

    What I was planning was to have the model\context on repository module and the entity classes as POCO on a global module that could be referenced by any other, but I could use some help on how to achieve this, can't seem to find a standard way to do this. Is it possible is it the right path?


    Tuesday, October 16, 2012 1:52 PM


  • Hi Ricardo,

    I think what you are after is Entity Framework Code First.

    With Code First you can create a context from any POCO class that you can reference in you data layer. The classes are just plain classes without anything special about them.

    Here is a walkthrough and video about using code first with an existing database: http://msdn.microsoft.com/en-us/data/jj200620

    Basically use code gen to create some classes to represent your data, then create a context that uses those plain classes.

    Let me know if that isn't what you are after or you have more issues. There is a bunch of other EF content on the EF site here: http://msdn.microsoft.com/en-US/data/ef

    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by Alexander Sun Tuesday, October 23, 2012 9:03 AM
    Thursday, October 18, 2012 8:08 PM