EntityLengths.Analyzer
1.0.1
dotnet add package EntityLengths.Analyzer --version 1.0.1
NuGet\Install-Package EntityLengths.Analyzer -Version 1.0.1
<PackageReference Include="EntityLengths.Analyzer" Version="1.0.1" />
<PackageVersion Include="EntityLengths.Analyzer" Version="1.0.1" />
<PackageReference Include="EntityLengths.Analyzer" />
paket add EntityLengths.Analyzer --version 1.0.1
#r "nuget: EntityLengths.Analyzer, 1.0.1"
#:package EntityLengths.Analyzer@1.0.1
#addin nuget:?package=EntityLengths.Analyzer&version=1.0.1
#tool nuget:?package=EntityLengths.Analyzer&version=1.0.1
EntityLengths.Analyzer
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)")]
- EF Core Fluent API configurations (
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 | Versions 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. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.8.0 && <= 4.12.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.