EntityLengths.Analyzer 1.0.1

dotnet add package EntityLengths.Analyzer --version 1.0.1
                    
NuGet\Install-Package EntityLengths.Analyzer -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntityLengths.Analyzer" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EntityLengths.Analyzer" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="EntityLengths.Analyzer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EntityLengths.Analyzer --version 1.0.1
                    
#r "nuget: EntityLengths.Analyzer, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EntityLengths.Analyzer@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EntityLengths.Analyzer&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=EntityLengths.Analyzer&version=1.0.1
                    
Install as a Cake Tool

EntityLengths.Analyzer

Made in Ukraine build EntityLengths.Generator NuGet current

Goals

A Roslyn analyzer that helps enforce string length constraints in your C# code. It analyzes property assignments and verifies they comply with length restrictions defined through attributes or Entity Framework configurations.

Terms of use

By using this project or its source code, for any purpose and in any shape or form, you grant your implicit agreement to all of the following statements:

  • You unequivocally condemn Russia and its military aggression against Ukraine
  • You recognize that Russia is an occupant that unlawfully invaded a sovereign state
  • You agree that Russia is a terrorist state
  • You fully support Ukraine's territorial integrity, including its claims over temporarily occupied territories
  • You reject false narratives perpetuated by Russian state propaganda

To learn more about the war and how you can help, click here. Glory to Ukraine! 🇺🇦

Features

  • Detects potential string length violations at compile time
  • Supports multiple ways of defining length constraints:
    • EF Core Fluent API configurations (HasMaxLength)
    • Data Annotations
      • [MaxLength]
      • [StringLength]
    • Column type definitions
      • [Column(TypeName = "varchar(200)")]
      • [Column(TypeName = "nvarchar(200)")]
      • [Column(TypeName = "char(200)")]

Installation

Install the library via NuGet Package Manager:

dotnet add package EntityLengths.Analyzer

Usage

Using Attributes

public class User
{
    [MaxLength(50)]
    public string Name { get; set; }  // Will show info/error if assigned string > 50 chars

    [StringLength(100)]
    public string Description { get; set; }  // Will show info/error if assigned string > 100 chars

    [Column(TypeName = "varchar(200)")]
    public string Url { get; set; }  // Will show info/error if assigned string > 200 chars
}

Using Entity Framework Fluent API

public class UserConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.Property(p => p.Name)
            .HasMaxLength(50);  // Will show warning/info if assigned string > 50 chars
    }
}

// Or in DbContext
public class UserDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
            .Property(b => b.Name)
            .HasMaxLength(50);
    }
}

Diagnostics

The analyzer provides two types of diagnostics:

  • ML001 (Info): Warns about potential length violations when assigning non-literal values
[MaxLength(5)]
public string Name { get; set; }

string value = GetValue();
Name = value;  // ML001: Property 'Name' has a maximum length of 5. Consider adding length validation.
  • ML002 (Error): Reports definite length violations with string literals
[MaxLength(5)]
public string Name { get; set; }

Name = "This is too long";  // ML002: String length (14) exceeds maximum length of 5 for property 'Name'
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 142 2/23/2025
1.0.0 104 2/22/2025