Up Code sample: Code First Walkthrough (WPF)
DevForce 2010 Resource Center » Code samples » Model code samples » Code sample: Code First Walkthrough (WPF) » Code sample: Code First Walkthrough: Separate the Model Project

Code sample: Code First Walkthrough: Separate the Model Project

Last modified on August 15, 2012 17:21

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.

SolutionExplorer-2-projs.png


Create the Model Project

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.

  • Tools | Library Package Manager | Package Manager Console Window
  • Enter at the prompt:

       Install-Package EntityFramework -ProjectName CodeFirstWalk.Model

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:

  • EntityFramework
  • IdeaBlade.Aop
  • IdeaBlade.Core
  • IdeaBlade.EntityModel
  • IdeaBlade.Linq
  • IdeaBlade.Validation
  • PostSharp
  • System.ComponentModel.DataAnnotations
  • System.Data.Entity

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”.

model3.png

11. Build the Model project only. It should compile cleanly.

Adjust the Application Project

1. Go to the CodeFirstWalk application project.

2. Delete the following Model-oriented files:

  • the “.ibmmx” file
  • DevForce.cf
  • Model.cs
  • Packages.config
  • ProductDbContext.cs

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.

Disable PostSharp in the application project

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"

DisablePostSharp.png

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.

Build and run [F5]

The application should run as it did before.

At the conclusion of Model separation, the solution structure should look like this:

model4.png

Optional steps

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.

  • Select all the references
  • Open the Properties Window
  • Find and change the “Specific Version” setting to “False”

2. Remove references you won’t need from the model project (optional).

  • Microsoft.CSharp
  • System.Data.DataSetExtensions
  • System.Xml
  • System.Xml.Linq

Remove references you won’t need from the application project (optional).

  • EntityFramework
  • IdeaBlade.AOP
  • System.Data.Entity
  • System.ComponentModel.DataAnnotations
The PostSharp library is still required in the application project although it is only used by the Model project. This is a peculiarity of WPF projects as explained above.

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. 

  • Add | New Item | Application Configuration File
  • Copy only the <connectionStrings> tag (and its contents) from the App.config in your application project (CodeFirstWalk).

See the Metadata Generation and Advanced database connections topics for a discussion of the issues.

Created by DevForce on October 28, 2011 11:35

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade