https://github.com/Atulin/AutoDbSet
Automagically create `DbSet`s in your `DbContext`
https://github.com/Atulin/AutoDbSet
csharp csharp-sourcegenerator dotnet
Last synced: about 2 months ago
JSON representation
Automagically create `DbSet`s in your `DbContext`
- Host: GitHub
- URL: https://github.com/Atulin/AutoDbSet
- Owner: Atulin
- License: mit
- Created: 2024-11-27T05:29:23.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2025-05-21T00:48:57.000Z (5 months ago)
- Last Synced: 2025-05-21T01:38:01.549Z (5 months ago)
- Topics: csharp, csharp-sourcegenerator, dotnet
- Language: C#
- Homepage:
- Size: 31.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- csharp-source-generators - AutoDbSet -   Source Generator to automatically create `DbSet<T>` properties on the `DbContext` from marked entities (Source Generators / Database / ORM)
README
[](https://www.nuget.org/packages/Atulin.AutoDbSet/)


[](./LICENSE)# AutoDbSet
Automagically add `DbSet`s to your `DbContext`
## Usage
Place `[AutoDbSet]` attribute on the database models you want to register...
```cs
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`...
```cs
using AutoDbSetGenerators;
using Microsoft.EntityFrameworkCore;namespace AutoDbSet.Demo;
[AutoDbContext]
public partial class MyCoolDbContext : DbContext
{
}
```And watch the magic happen!
```cs
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 Persons { get; init; }
}
```## DbSet naming
By default `AutoDbSet` will try to naively pluralize the names ([here's how](./AutoDbSet/NameHelpers.cs)). 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:
```cs
[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
```cs
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 People { get; init; }
}
```## Caveats
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.