RSCG – EntityLengths.Generator

RSCG – EntityLengths.Generator
 
 

name EntityLengths.Generator
nuget https://www.nuget.org/packages/EntityLengths.Generator/
link https://github.com/TarasKovalenko/EntityLengths.Generator/
author Taras Kovalenko

Generating constants for max length for properties in entities

 

This is how you can use EntityLengths.Generator .

The code that you start with is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
<Project Sdk="Microsoft.NET.Sdk">
 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.1" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.1">
        </PackageReference>
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
        <PackageReference Include="EntityLengths.Generator" Version="1.0.3" />
 
    </ItemGroup>
    <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>
</Project>

The code that you will use is

1
2
3
4
5
6
7
8
Console.WriteLine("Hello, World!");
DbContextOptionsBuilder<DotNetStatsContext> optionsBuilder = new();
optionsBuilder.UseInMemoryDatabase("StatsDatabase");
var cnt = new DotNetStatsContext(optionsBuilder.Options);
await cnt.Database.EnsureCreatedAsync();
Console.WriteLine("Database created");
Console.WriteLine(cnt.Projects.Count());
Console.WriteLine("The max length of the Name property of the Project entity is: " + Constants.Project.NameLength);
01
02
03
04
05
06
07
08
09
10
11
12
13
14
global using Microsoft.EntityFrameworkCore;
global using Stats.Database;
using EntityLengths.Generator.Configuration;
 
[assembly: EntityLengthsGenerator(
    GenerateDocumentation = false,
    GeneratedClassName = "Constants",
    LengthSuffix = "Length",
    IncludeNamespaces = ["Stats.Database"],
    ExcludeNamespaces = [],
    ScanNestedNamespaces = true,
    ScanEntitySuffix = null,
    Namespace = "Stats.Database"
)]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace Stats.Database;
public partial class DotNetStatsContext : DbContext
{
    internal DotNetStatsContext() : base() { }
    public DotNetStatsContext(DbContextOptions<DotNetStatsContext> options)
        : base(options)
    {
         
    }
     
    public virtual DbSet<Project> Projects { get; set; }
 
    public virtual DbSet<Star> Stars { get; set; }
 
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Project>(entity =>
        {
            entity.ToTable("Project");
 
            entity.Property(e => e.Id).HasColumnName("ID");
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Name).HasMaxLength(50);
            entity.Property(e => e.SourceCodeUrl).HasMaxLength(50);
        });
 
        modelBuilder.Entity<Star>(entity =>
        {
            entity.Property(e => e.Id)
                .HasColumnName("ID");
            entity.Property(e => e.Idproject).HasColumnName("IDProject");
        });
 
        OnModelCreatingPartial(modelBuilder);
    }
 
    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

 

The code that is generated is

01
02
03
04
05
06
07
08
09
10
11
12
// <auto-generated/>
namespace Stats.Database;
 
public static partial class Constants
{
    public static partial class Project
    {
        public const int DescriptionLength = 500;
        public const int NameLength = 50;
        public const int SourceCodeUrlLength = 50;
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/EntityLengths.Generator