Many of us prefer to keep our entity models in their own projects, separate from other applications projects … especially from the UI projects.
Of course we could have begun development of the “Code First Walk” application with an independent model project. But we didn’t because we were keeping it as simple as possible … as you might do.
Fortunately, it’s easy to move the model to its own project … as we do here.
1. Close all Visual Studio editor windows (Alt-W, L).
2. Add | New Project | Class Library
3. Name it “CodeFirstWalk.Model”.
4. Delete the template-generated “Class1”.
5. Add Entity Framework Code First Library reference.
The console should display
PM> Install-Package EntityFramework -ProjectName CodeFirstWalk.Model 'EntityFramework 4.2.0.0' already installed. Successfully added 'EntityFramework 4.2.0.0' to CodeFirstWalk.Model. |
6. Add the other references.
CodeFirstWalk also requires references to these libraries:
The easy way to add them is to let the "DevForce CodeFirst File" item template do it for you while it adds the marker file.
7. Add | New Item (or [ctrl+shift+A]) | DevForce 2010 | DevForce CodeFirst File | "DevForce.cf"
8. Copy the Model and ProductDbContext files in the CodeFirstWalk application project into the Model project.
9. Open the Model file.
10. Change all Model file namespaces to CodeFirstWalk.Model by using (Ctrl-H) “Global Replace” (for the model project only).
Replace all “namespace CodeFirstWalk” with “namespace CodeFirstWalk.Model”.
11. Build the Model project only. It should compile cleanly.
1. Go to the CodeFirstWalk application project.
2. Delete the following Model-oriented files:
3. Add project reference to CodeFirstWalk.Model.
4. Build the solution.
It will fail, complaining that it can’t find the model classes. The error messages tell you where you need to add “using CodeFirstWalk.Model;”. In our example, you only need to …
5. Add “using CodeFirstWalk.Model;” to MainWindowViewModel.
At build, PostSharp investigates the classes of every project in which it is referenced, directly or indirectly. That's a waste of time. We can disable PostSharp analysis by flipping a switch.
1. Open the application project property editor (Alt-Enter)
2. Open the PostSharp tab near the bottom
3. Set "Disable PostSharp" to "Yes"
In principle, you shouldn’t need a reference to PostSharp in the application project because you aren’t compiling any AOP classes in there; you are merely consuming AOP objects defined in the Model assembly. In practice, you must reference PostSharp.dll in WPF projects. This is only a problem for WPF projects.
The application should run as it did before.
At the conclusion of Model separation, the solution structure should look like this:
1. Consider setting “Specific Version=False” for all project references.
The "Specific Version = True” for most of the added references (no matter how you added them). DevForce releases new versions of its libraries every 6 to 10 weeks. If you upgrade frequently when "Specific Version = True", you’ll notice that your project with IdeaBlade references don’t compile until you upgrade them individually … which is painful. We like to set this flag to “False” during development.
2. Remove references you won’t need from the model project (optional).
Remove references you won’t need from the application project (optional).
3. Add an App.config file if necessary (optional).
If Entity Framework is (re)creating the development database for you (as we’ve asked it to do in this CodeFirstWalk sample) then you don’t need to add an App.config.
If you require design time access to a specific database - as you might if you hadn’t installed SQL Server Express on your development machine -, you may need to create an App.config file in the Model project.
See the Metadata Generation and Advanced database connections topics for a discussion of the issues.