Automagically add DbSet<T>s to your DbContext
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", "1.0.0.0")]
public partial class MyCoolDbContext
{
public required Microsoft.EntityFrameworkCore.DbSet<AutoDbSet.Demo.Person> Persons { get; init; }
}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; }
}The generator only works when there's a single DbContext with the attribute in the project.
I plan to add support for multiple contexts in a future update.