Atulin.AutoDbSet by Angius Atulin
NuGet / site data
Details
Info
Name: Atulin.AutoDbSet
Package Description
Author: Angius Atulin
NuGet: https://www.nuget.org/packages/Atulin.AutoDbSet/
You can find more details at https://github.com/Atulin/AutoDbSet
Author
Angius Atulin

Original Readme
AutoDbSet
Automagically add DbSet<T>s to your DbContext
Usage
Place [AutoDbSet] attribute on the database models you want to register...
using AutoDbSetGenerators;
namespace AutoDbSet.Demo;
[AutoDbSet]
public class Person
{
public required string Name \{ get; set; }
public required DateOnly Birthday \{ get; set; }
public required float Height \{ get; set; }
}
Place [AutoDbContext] attribute on your DbContext and make it partial...
using AutoDbSetGenerators;
using Microsoft.EntityFrameworkCore;
namespace AutoDbSet.Demo;
[AutoDbContext]
public partial class MyCoolDbContext : DbContext
{
}
And watch the magic happen!
namespace AutoDbSet.Demo;
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.CodeDom.Compiler.GeneratedCode("AutoDbSet", "2.0.0.0")]
public partial class MyCoolDbContext
{
public required Microsoft.EntityFrameworkCore.DbSet<AutoDbSet.Demo.Person> Persons \{ get; init; }
}
DbSet naming
By default AutoDbSet will try to naively pluralize the names (here's how). It does not,
therefore, work with verbs that have irregular plural form, nor does it work with non-English languages.
You can, however, give the sets your own, custom name:
[AutoDbSetGenerators.AutoDbSet(Name = "People")]
public class Person
{
public required string Name \{ get; set; }
public required DateOnly Birthday \{ get; set; }
public required float Height \{ get; set; }
}
will generate
namespace AutoDbSet.Demo;
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.CodeDom.Compiler.GeneratedCode("AutoDbSet", "1.0.0.0")]
public partial class MyCoolDbContext
{
public required Microsoft.EntityFrameworkCore.DbSet<AutoDbSet.Demo.Person> People \{ get; init; }
}
Multiple DbContexts support
You can relate a given class to a given DbContext using the DbContext property of the
[AutoDbSet] attribute:
[AutoDbSetGenerators.AutoDbSet(DbContext = typeof(MyCoolerDbContext))]
public class CoolerPerson
{
public required string Name \{ get; set; }
public required DateOnly Birthday \{ get; set; }
public required float Height \{ get; set; }
public bool IsCool \{ get; \} = true;
}
Classes without an explicit DbContext will be registered to the single DbContext that
was not specified in any other [AutoDbSet] attribute.
[!IMPORTANT] There can only be a single default
DbContext
ExpressiveSharp support
AutoDbSet supports ExpressiveSharp out of the box,
and generates ExpressiveDbSet<T>s instead of DbSet<T>s if it's available.
About
Generating DbSet properties for Entity Framework Core DbContext classes
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- Project.cs
- DotNetStatsContext.cs
This is the CSharp Project that references Atulin.AutoDbSet
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Atulin.AutoDbSet" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.11" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of Atulin.AutoDbSet in Program.cs
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
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());
This is the use of Atulin.AutoDbSet in Project.cs
namespace Stats.Database;
[AutoDbSetGenerators.AutoDbSet]
public partial class Project
{
public long Id \{ get; set; }
public string Name \{ get; set; \} = null!;
public string SourceCodeUrl \{ get; set; \} = null!;
public string? Description \{ get; set; }
}
This is the use of Atulin.AutoDbSet in DotNetStatsContext.cs
using System.Diagnostics.CodeAnalysis;
namespace Stats.Database;
[AutoDbSetGenerators.AutoDbContext]
public partial class DotNetStatsContext : DbContext
{
[SetsRequiredMembers]
internal DotNetStatsContext() : base() {
Stars = Set<Star>();
Projects = Set<Project>();
}
[SetsRequiredMembers]
public DotNetStatsContext(DbContextOptions<DotNetStatsContext> options)
: base(options)
{
Stars = Set<Star>();
Projects = Set<Project>();
}
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);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- AutoDbSetAttribute.g.cs
- DbContextAttribute.g.cs
- DotNetStatsContext.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool AutoDbSet.
// Runtime Version: 2.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
namespace AutoDbSetGenerators;
/// <summary>
/// Marks the class to generate a DbSet out of
/// </summary>
[global::System.AttributeUsage(System.AttributeTargets.Class)]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.CodeDom.Compiler.GeneratedCode("AutoDbSet", "2.0.0.0")]
public sealed class AutoDbSetAttribute : global::System.Attribute
{
/// <summary>
/// Sets name to be used in the database.
/// </summary>
public string? Name \{ get; set; }
/// <summary>
/// DbContext this DbSet should belong to
/// </summary>
public global::System.Type? DbContext \{ get; set; }
}
#nullable restore
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool AutoDbSet.
// Runtime Version: 2.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
namespace AutoDbSetGenerators;
/// <summary>
/// Marks the DbContext to generate DbSets into
/// </summary>
[global::System.AttributeUsage(System.AttributeTargets.Class)]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.CodeDom.Compiler.GeneratedCode("AutoDbSet", "2.0.0.0")]
public sealed class AutoDbContextAttribute : global::System.Attribute
{}
#nullable restore
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool AutoDbSet.
// Runtime Version: 2.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
namespace Stats.Database;
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.CodeDom.Compiler.GeneratedCode("AutoDbSet", "2.0.0.0")]
public partial class DotNetStatsContext
{
public required Microsoft.EntityFrameworkCore.DbSet<Stats.Database.Project> Projects \{ get; init; }
public required Microsoft.EntityFrameworkCore.DbSet<Stats.Database.Star> Stars \{ get; init; }
}
#nullable restore
Useful
Download Example (.NET C#)
Share Atulin.AutoDbSet
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Atulin.AutoDbSet
Category "EntityFramework" has the following generators:
1 Atulin.AutoDbSet
2026-08-18
2 EntityLengths.Generator
2025-02-19