DataContext base class name

Posts   
 
    
IanElsinga
User
Posts: 13
Joined: 12-May-2017
# Posted on: 14-Sep-2022 02:11:42   

Hi, I am using LLBLGen Pro v5.9 with EF Core.

I am using Audit.EnitityFramework to provide automatic auditing on everything that is saved from the DbContext. This means that the DataContext class must inherit from AuditDbContext, not from DbContext. Every time I generate the persistence code, it overwrites this. How can I specify the DbContext class to use and also include the appropriate using statement?

Thanks much, Ian

//------------------------------------------------------------------------------
// <auto-generated>This code was generated by LLBLGen Pro v5.9.</auto-generated>
//------------------------------------------------------------------------------
using System;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MyData.EntityClasses;
using Audit.EntityFramework; //<-------

namespace MyData
{
    /// <summary>Class which represents the DataContext for the project / group </summary>
    public partial class MyDataContext : AuditDbContext //<--------
    {
...
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 14-Sep-2022 09:24:41   

To change that base class, you have to alter the template. This is specified in the <llblgen pro installation folder>\Frameworks\Entity Framework\Templates\VCore\C#\contextclass.lpt, on line 37.

To use your own custom version of this template, you can either change the template in the installation folder, or create a copy of it, and bind it in a custom templatebindings file. See https://www.llblgen.com/Documentation/5.9/Designer/How%20Toworkwithtemplates.htm, https://www.llblgen.com/Documentation/5.9/Designer/Functionality%20Reference/TemplateBindingsEditor.htm and https://www.llblgen.com/Documentation/5.9/SDK/TemplatesandTemplatebindings.htm

It might look daunting but it's fairly straightforward. Using a copy of the template can be beneficial (as you can keep using your version when you migrate to a newer v5.9.x), however you have to keep track of the changes made to the original template in future versions. (I don't expect many changes in this template however, but you'll never know).

Just out of curiosity: the AuditDbContext overrides certain methods which are also overridden by the generated class? As you also perhaps could make AuditDbContext a partial class of the generated context?

Frans Bouma | Lead developer LLBLGen Pro
IanElsinga
User
Posts: 13
Joined: 12-May-2017
# Posted on: 22-Sep-2022 18:15:58   

Thank you very much, Otis, that worked perfectly. I don't think AuditDBContext overrides any methods - it's operations are hidden completely by the replaced DbContext (in my understanding).

Appreciate your help. Ian