EF6-CodeFirst: How to have different namespace for persistent & model project

Posts   
 
    
pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 25-Apr-2017 08:45:39   

Our code requires both projects (persistent & model in EF6 Code First) to have a different namespace. How could the persistence project have Northwind.Tutorial.DAL for example please?

namespace Northwind.Tutorial
{
    /// <summary>Class which represents the DataContext for the project / group 'Northwind'</summary>
    public partial class NorthwindDataContext : DbContext
namespace Northwind.Tutorial.EntityClasses
{
    /// <summary>Class which represents the entity 'Category'.</summary>
    [Serializable]
    [DataContract(IsReference=true)]
    public partial class Category : CommonEntityBase

Thanks, Patrick

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 25-Apr-2017 11:34:26   

You can specify per vs project additional folders and namespaces. To do that you have to create a new preset, which is based on the original one. To do this, go into the designer and load your project. Go into project settings and for 'Additional tasks' folder you specify .\ This will mean the preset file you're going to create is placed locally with the project.

Now, in the designer, open the Preset Viewer (Tools -> Preset Viewer). Select in the Name combo box the 'SD.EntityFramework.v6 (Code First)' preset and click 'Copy As New...' button next to it. A dialog opens. Specify a name for it so it's unique and you know what it does and specify the target. You'll see the folders known to the designer where preset files can be located, among them the .\ folder you specified. Select that one and click OK

The XML editor opens and you'll see the preset contents. Now go to line 19, where you see:

    <taskGroupPreset name="SD.Tasks.Base.GenericTaskGroup" displayName="Generate Entity Framework Domain model with POCO entities" requiresCleanCache="false" forSpecificDatabase="">

Add the attribute (it has intellisense, just type a space and you'll get a list of additional attributes to add): additionalRootNamespace="YourAdditionalModelProjectNamespace"

You'll do the same for the persistence vs project, at line 80:

    <taskGroupPreset name="SD.Tasks.Base.GenericTaskGroup" displayName="Generate Entity Framework Database specific code and code first mapping file using DbContext API and POCO entities" forSpecificDatabase="All" requiresCleanCache="true">

Add the same attribute, additionalRootNamespace="YourAdditionalPersistenceProjectNamespace"

You can also decide to just add an additional namespace for the persistence project and leave line 19 as-is.

Save the file with cntrl-shift-s. Generate your code, be sure to select the new preset in the code generation task, and not the default one wink . (this is kept in the project file so next time the same one is selected, so no need to edit it every time).

Your copy isn't overwritten when you install a new llblgen pro version. A preset drives the tasks for the code generator and they in general don't change.

Frans Bouma | Lead developer LLBLGen Pro
pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 26-Apr-2017 18:58:17   

Sounds great thank you