The constructor I emit in the adhoc template passes in the connection string to the constructor of the datacontext which I added in a partial class. If you don't do it that way, you can remove that constructor.
<[SD.Tools.BCLExtensions.CollectionsRelated]>
<%
Project currentProject = _executingGenerator.ProjectDefinition;
%>//------------------------------------------------------------------------------
// <auto-generated>This code was generated by LLBLGen Pro v<%=ApplicationConstants.LLBLGenProVersion%>.</auto-generated>
//------------------------------------------------------------------------------
using System;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using <%=_executingGenerator.RootNamespaceToUse%>.EntityClasses;
namespace <%=_executingGenerator.RootNamespaceToUse%>
{
public class <%=_executingGenerator.ActiveSourceElementsContainerName%>DataContextDerived : <%=_executingGenerator.ActiveSourceElementsContainerName%>DataContext
{
public <%=_executingGenerator.ActiveSourceElementsContainerName%>DataContextDerived(string connectionString) : base(connectionString)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
<%
foreach(var entity in _executingGenerator.Entities.OrderBy(v=>v.FullName, ApplicationUtils.GetNameOrderingComparer()))
{
var mapping = currentProject.GetGroupableModelElementMapping(entity, _executingGenerator.DriverID);
foreach(var fieldMapping in mapping.FieldMappings.Where(fm=>(!fm.MappedFieldInstance.IsDiscriminator && (fm.MappedFieldWrapper.Path.Count<=1)) ||
fm.MappedFieldInstance.IsPartOfIdentifyingFields).OrderBy(fm=>fm.MappedTarget.OrdinalPosition))
{
var field = fieldMapping.MappedFieldInstance as IFieldElementCore;
if(field==null || field.FieldType.KindOfType!=FieldTypeKind.DotNetType)
{
continue;
}
if(field.FieldType.RepresentedType==typeof(DateTime))
{
%> modelBuilder.Entity<<%=entity.Name%>>().Property(t => t.<%=field.Name%>).HasColumnType("datetime");
<% }
}
}
%> }
}
}
Using adhoctemplate file:
<adhocTemplates usedByModelType="1">
<template filename="AppendHasColumnTypeToFields.lpt" outputFilename=".\Persistence\[containerName]DataContextDerived.[extension]" emitType="generic"/>
</adhocTemplates>
to enable the adhoc templates, double click the code generation task and at the bottom of the dialog that opens you can check the enable checkbox for the adhoc template file (after you reloaded the project).
You have to do that once, it's persisted with the project after a successful code generation run. The example above only emits fields with datetime and doesn't do specific checking on fk fields being omitted and the like but I think it's not applicable here anyway.