1

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?

Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100

1 Answers1

1

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.

Community
  • 1
  • 1
MaxSC
  • 4,698
  • 2
  • 23
  • 36
  • That's a really interesting quote - I didn't know that. Writing things myself might be a way to go, but what if my database is large? That's a lot to write. Also, I quite like the designer features (visualisation, easy mapping). Is it possible to eat my cake and have it too? Can I use the ADO.NET generator and still split things up in a meaningful way? – Rowan Freeman Jul 16 '13 at 22:54
  • 1
    There's allmost nothing to write as your code will be generated. And if your DB is large, well, you're gonna have a lot of generated classes! The EDM designer is cool but it won't let you do all the things you could do using code first approach. You can still split things up in a meaningful way with the EDMX, you just need to move the T4 template that generates your model in a separate project and edit it to let it find the source EDMX at the right place. But keep in mind that working with EDMX might be less testable. – MaxSC Jul 17 '13 at 05:38
  • Unfortunately the two Visual Studio extensions that I've found that assist with this approach: Entity Framework Power Tools and "CodeSecond" are C# only, and I'm using VB.NET. – Rowan Freeman Jul 18 '13 at 00:10
  • 1
    Well, you can mix VB.Net and C# assemblies within the same solution. Generated classes are pretty straightforward, it won't be a pb to have them in C# and the other layers coded in VB.Net. But if you're really reluctant to C# there are tools to transform C# to VB.Net and vice versa! – MaxSC Jul 18 '13 at 07:36
  • I prefer C# and I can use both easily, but my colleagues use VB.NET. Good suggestions though, I might just do that. – Rowan Freeman Jul 18 '13 at 08:10