Pergunta

According to onion architecture I should have my domain models (User, Student, Teacher) in my "Core" or "Domain" layer and my DbContext in my "Infrastructure" layer.

I use Entity Framework 5 in my MVC4 application and I'm using ADO.NET database-first approach (the database for the application already exists). Since ADO.NET generates the entities for me (.edmx file), how do I separate the domain from the database layer?

At the moment I have my entities model in the Core, but doesn't that break the architecture since the Core shouldn't know about how the data is accessed?

Foi útil?

Solução

Well, when EF guys decided to name the different ways building an application based on EF, they did not choose the best wording!

According to Rowan Miller himself:

Admittedly ‘Code First’ wasn’t the best choice of names… Code First is really just a code-based alternate to the EF Designer, it supports creating a new database or mapping to an existing database. Using Code First against an existing database is often called ‘Code Second’ because… well… you have the database first and then write the code second.

That said, you know now that it's possible to get rid of the EDMX even if you start your project with an existing DB.
Have a look at at that article for a step by step procedure.

As soon as everything is generated, it's just a matter of putting things at the right place to keep your Core layer agnostic.

Have a look at this SO answer where I helped someone having a similar issue.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top