Skip to content

Repository files navigation

NuGet VersionNuGet DownloadsGitHub Actions Workflow StatusGitHub License

AutoDbSet

Automagically add DbSet<T>s to your DbContext

Usage

Place [AutoDbSet] attribute on the database models you want to register...

usingAutoDbSetGenerators;namespaceAutoDbSet.Demo;[AutoDbSet]publicclassPerson{publicrequiredstringName{get;set;}publicrequiredDateOnlyBirthday{get;set;}publicrequiredfloatHeight{get;set;}}

Place [AutoDbContext] attribute on your DbContext and make it partial...

usingAutoDbSetGenerators;usingMicrosoft.EntityFrameworkCore;namespaceAutoDbSet.Demo;[AutoDbContext]publicpartialclassMyCoolDbContext:DbContext{}

And watch the magic happen!

namespaceAutoDbSet.Demo;[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute][global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage][global::System.CodeDom.Compiler.GeneratedCode("AutoDbSet","2.0.0.0")]publicpartialclassMyCoolDbContext{publicrequiredMicrosoft.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")]publicclassPerson{publicrequiredstringName{get;set;}publicrequiredDateOnlyBirthday{get;set;}publicrequiredfloatHeight{get;set;}}

will generate

namespaceAutoDbSet.Demo;[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute][global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage][global::System.CodeDom.Compiler.GeneratedCode("AutoDbSet","1.0.0.0")]publicpartialclassMyCoolDbContext{publicrequiredMicrosoft.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))]publicclassCoolerPerson{publicrequiredstringName{get;set;}publicrequiredDateOnlyBirthday{get;set;}publicrequiredfloatHeight{get;set;}publicboolIsCool{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

Automagically create `DbSet`s in your `DbContext`

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages