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
<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
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);
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"
)]
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
// <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
Leave a Reply