LLBLGen Pro Support System Designer feed https://llblgen.com/tinyforum//Forum/8 This is the RSS feed for the forum Designer on the LLBLGen Pro Support System forum system. 30 en-us How to add .HasColumnType("datetime") to date fields by JoeE <p>Thank you, Frans. Really appreciate the help.</p> JoeE https://llblgen.com/tinyforum/Thread/28847#153414 Fri, 01 Mar 2024 09:00:30 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153414 How to add .HasColumnType("datetime") to date fields by Otis <p>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. </p> <pre><code class="cs">&lt;[SD.Tools.BCLExtensions.CollectionsRelated]&gt; &lt;% Project currentProject = _executingGenerator.ProjectDefinition; %&gt;//------------------------------------------------------------------------------ // &lt;auto-generated&gt;This code was generated by LLBLGen Pro v&lt;%=ApplicationConstants.LLBLGenProVersion%&gt;.&lt;/auto-generated&gt; //------------------------------------------------------------------------------ using System; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using &lt;%=_executingGenerator.RootNamespaceToUse%&gt;.EntityClasses; namespace &lt;%=_executingGenerator.RootNamespaceToUse%&gt; { public class &lt;%=_executingGenerator.ActiveSourceElementsContainerName%&gt;DataContextDerived : &lt;%=_executingGenerator.ActiveSourceElementsContainerName%&gt;DataContext { public &lt;%=_executingGenerator.ActiveSourceElementsContainerName%&gt;DataContextDerived(string connectionString) : base(connectionString) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); &lt;% foreach(var entity in _executingGenerator.Entities.OrderBy(v=&gt;v.FullName, ApplicationUtils.GetNameOrderingComparer())) { var mapping = currentProject.GetGroupableModelElementMapping(entity, _executingGenerator.DriverID); foreach(var fieldMapping in mapping.FieldMappings.Where(fm=&gt;(!fm.MappedFieldInstance.IsDiscriminator &amp;&amp; (fm.MappedFieldWrapper.Path.Count&lt;=1)) || fm.MappedFieldInstance.IsPartOfIdentifyingFields).OrderBy(fm=&gt;fm.MappedTarget.OrdinalPosition)) { var field = fieldMapping.MappedFieldInstance as IFieldElementCore; if(field==null || field.FieldType.KindOfType!=FieldTypeKind.DotNetType) { continue; } if(field.FieldType.RepresentedType==typeof(DateTime)) { %&gt; modelBuilder.Entity&lt;&lt;%=entity.Name%&gt;&gt;().Property(t =&gt; t.&lt;%=field.Name%&gt;).HasColumnType(&quot;datetime&quot;); &lt;% } } } %&gt; } } } </code></pre> <p>Using adhoctemplate file: </p> <pre><code class="xml">&lt;adhocTemplates usedByModelType=&quot;1&quot;&gt; &lt;template filename=&quot;AppendHasColumnTypeToFields.lpt&quot; outputFilename=&quot;.\Persistence\[containerName]DataContextDerived.[extension]&quot; emitType=&quot;generic&quot;/&gt; &lt;/adhocTemplates&gt; </code></pre> <p>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). </p> <p>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. </p> Otis https://llblgen.com/tinyforum/Thread/28847#153413 Fri, 01 Mar 2024 08:44:11 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153413 How to add .HasColumnType("datetime") to date fields by Otis <p>Changing the template in the designer is indeed not the best way, as an update will overwrite it. Additionally, using a copy of it, using your own template bindings file (so basically you use your copy of the template instead of the one shipped with the designer) might be better, but then too you won't take into account any changes we make to the template (as you're using your own copy). </p> <p>So the best way to fix this I think is to generate the derived context class as I gave an example of above. You can do that using two ways. One being adhoc templates, see: <a href="https://www.llblgen.com/Documentation/5.11/Designer/Functionality%20Reference/AdHocTemplates.htm" rel="nofollow">https://www.llblgen.com/Documentation/5.11/Designer/Functionality%20Reference/AdHocTemplates.htm</a> They're basically templates defined in a folder using a simple xml file which are run with every code generation action. When you write your derived class template in an adhoc template it should keep working when you update the designer. An alternative is to define a new template with a custom template bindings file (See: <a href="https://www.llblgen.com/Documentation/5.11/Designer/Functionality%20Reference/TemplateBindingsViewer.htm" rel="nofollow">https://www.llblgen.com/Documentation/5.11/Designer/Functionality%20Reference/TemplateBindingsViewer.htm</a>) but that's more steps and for a single template likely overkill due to the complexity involved. </p> <p>I'll write an example template to generate the class above and post it in a separate post. </p> Otis https://llblgen.com/tinyforum/Thread/28847#153412 Fri, 01 Mar 2024 08:16:09 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153412 How to add .HasColumnType("datetime") to date fields by JoeE <p>I've changed the <em>ModelBuilder.lpt</em> to add the HasColumnType() as you suggested, and it solved the issue on my side.</p> <p>Though I am worried that when there is a new version of the designer, it might override the <em>.lpt</em> file and thus I loose my changes (I might have gone about it wrong, but I've changed the file it its original install location?).</p> <p>Which brings me to your suggestion of an &quot;adhoc template&quot;. I like this more. Can you perhaps point me in the right direction as to where to write it and hook it up (even a link to docs or examples will help).</p> JoeE https://llblgen.com/tinyforum/Thread/28847#153403 Thu, 29 Feb 2024 08:01:12 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153403 How to add .HasColumnType("datetime") to date fields by Otis <p>The github thread suggests the issue was in EF Core and they've fixed it, but apparently it's not fixed in your situation? </p> <p>There's another way: generating a derived type of the generated context class and in there generate an override of OnModelCreating. First call the base method, and then generate for each entity type, and for each entity type for each datetime field, the HasColumnType(&quot;datetime&quot;). You could do this using an adhoc template for that so you don't need templatebindings if you don't want to. If it's just a few fields, you can also manually write these out. It should look like this:</p> <pre><code class="cs">public class EFCore8NWDataContextDerived : EFCore8NWDataContext { public EFCore8NWDataContextDerived(string connectionString) : base(connectionString) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity&lt;Order&gt;().Property(t =&gt; t.OrderDate).HasColumnType(&quot;datetime&quot;); } } </code></pre> <p>Here I add the column type to the Order entity's OrderDate field. To use this, use the derived context class instead of the generated context class. I wanted to use the partial class method OnModelCreatingComplete, but we made a design error there and don't pass the ModelBuilder object to that partial method <img src="/tinyforum/pics/emojis/flushed.png" class="emoji" alt="flushed"/></p> <p>If you have problems writing the template, please let us know. </p> Otis https://llblgen.com/tinyforum/Thread/28847#153402 Thu, 29 Feb 2024 07:46:41 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153402 How to add .HasColumnType("datetime") to date fields by JoeE <blockquote><p class="quote-nickname">Otis wrote:</p> <p>We don't generate these in the ModelBuilder class as they're not needed at runtime and our designer does a better job at exporting DDL SQL. Is there a particular reason you need this btw? I'm afraid if you really want this, you'll have to use a customized ModelBuilder.lpt template. The function which determines which functions to append to a property mapping starts at line 409 (<code>DeterminePropertyAppendCalls</code>)</p> </blockquote> <p>Thanks for the prompt response.</p> <p>The request relate to a problem we experienced using DevExtreme's DataSourceLoader. When we try to filter on a date field, we receive an exception &quot;Conversion failed when converting date and/or time from character string&quot;. Adding the .HasColumnType is the suggested solution, which I've tried, and it worked.</p> <p>The link to the DevExpress support ticket <a href="https://supportcenter.devexpress.com/ticket/details/t737816/datagrid-the-conversion-failed-when-converting-date-and-or-time-from-character-string" rel="nofollow">https://supportcenter.devexpress.com/ticket/details/t737816/datagrid-the-conversion-failed-when-converting-date-and-or-time-from-character-string</a> which in turn reference this EF Core issue <a href="https://github.com/dotnet/efcore/issues/14095#issuecomment-464365442" rel="nofollow">https://github.com/dotnet/efcore/issues/14095#issuecomment-464365442</a></p> <p>From what I can see this would not be an issue if the MSSQL Data type is datetime2, though I have not tested this myself. </p> JoeE https://llblgen.com/tinyforum/Thread/28847#153399 Wed, 28 Feb 2024 08:14:00 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153399 How to add .HasColumnType("datetime") to date fields by Otis <p>We don't generate these in the ModelBuilder class as they're not needed at runtime and our designer does a better job at exporting DDL SQL. Is there a particular reason you need this btw? I'm afraid if you really want this, you'll have to use a customized ModelBuilder.lpt template. The function which determines which functions to append to a property mapping starts at line 409 (<code>DeterminePropertyAppendCalls</code>)</p> Otis https://llblgen.com/tinyforum/Thread/28847#153398 Wed, 28 Feb 2024 07:01:41 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153398 How to add .HasColumnType("datetime") to date fields by JoeE <blockquote><p class="quote-nickname">Walaa wrote:</p> <p>Which LLBLGen Pro Designer version are you using?</p> <p>Which framework are you targeting? is it E.F.? which version?</p> </blockquote> <p>Sorry, neglected to include that: Designer version: 5.11 (5.11.0) RTM Target Framework: EF Core v8</p> <p>Regards,</p> <p>Joe</p> JoeE https://llblgen.com/tinyforum/Thread/28847#153395 Wed, 28 Feb 2024 04:47:31 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153395 How to add .HasColumnType("datetime") to date fields by Walaa <p>Which LLBLGen Pro Designer version are you using?</p> <p>Which framework are you targeting? is it E.F.? which version?</p> Walaa https://llblgen.com/tinyforum/Thread/28847#153393 Wed, 28 Feb 2024 00:30:34 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153393 How to add .HasColumnType("datetime") to date fields by JoeE <p>Hi,</p> <p>How can I have my date fields be generated with .HasColumnType(&quot;datetime&quot;)? </p> <p>Regards,</p> <p>Joe</p> JoeE https://llblgen.com/tinyforum/Thread/28847#153392 Tue, 27 Feb 2024 15:31:27 GMT How to add .HasColumnType("datetime") to date fields https://llblgen.com/tinyforum/Thread/28847#153392