Visium.Anima.EntityFrameworkCore.SourceGeneration
1.0.0-alpha.10
See the version list below for details.
dotnet add package Visium.Anima.EntityFrameworkCore.SourceGeneration --version 1.0.0-alpha.10
NuGet\Install-Package Visium.Anima.EntityFrameworkCore.SourceGeneration -Version 1.0.0-alpha.10
<PackageReference Include="Visium.Anima.EntityFrameworkCore.SourceGeneration" Version="1.0.0-alpha.10" />
paket add Visium.Anima.EntityFrameworkCore.SourceGeneration --version 1.0.0-alpha.10
#r "nuget: Visium.Anima.EntityFrameworkCore.SourceGeneration, 1.0.0-alpha.10"
// Install Visium.Anima.EntityFrameworkCore.SourceGeneration as a Cake Addin #addin nuget:?package=Visium.Anima.EntityFrameworkCore.SourceGeneration&version=1.0.0-alpha.10&prerelease // Install Visium.Anima.EntityFrameworkCore.SourceGeneration as a Cake Tool #tool nuget:?package=Visium.Anima.EntityFrameworkCore.SourceGeneration&version=1.0.0-alpha.10&prerelease
Anima.EntityFrameworkCore.SourceGeneration
Source generators to reduce boilerplate code when working with EntityFrameworkCore.
GenerateDbSets
This feature eases the burden of manually adding entities to the DbContext
every time a new entity
class is created.
Usage
Set the project's DbContext
class as partial
and mark it with the [GenerateDbSets]
attribute to automatically generate DbSet
properties for every class
in the project that implements IEntityTypeConfiguration
.
Example
Given the following classes:
[GenerateDbSets]
public partial class DatabaseContext : DbContext
{
public DatabaseContext() { } // Parameterless constructor for dotnet ef
public DatabaseContext(DbContextOptions<DatabaseContext> options)
{
// Configure the database here
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Add entity configurations from classes that implement IEntityTypeConfiguration
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetAssembly(typeof(DatabaseContext))!);
base.OnModelCreating(modelBuilder);
}
}
public class User : IEntityTypeConfiguration<User>
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Session> Sessions { get; set; } = new HashSet<Session>();
public void Configure(EntityTypeBuilder<User> builder)
{
builder.Property(e => e.FirstName).HasMaxLength(50);
builder.Property(e => e.LastName).HasMaxLength(50);
}
}
public class Session : IEntityTypeConfiguration<User>
{
public int Id { get; set; }
public User User { get; set; }
public int UserId { get; set; }
public string Platform { get; set; }
public DateTime LoginTime { get; set; }
public DateTime? LogoutTime { get; set; }
public void Configure(EntityTypeBuilder<User> builder)
{
builder.Property(e => e.Platform).HasMaxLength(20);
}
}
This output will be generated:
public partial class DatabaseContext
{
public DbSet<User> Users { get; set; }
public DbSet<Session> Sessions { get; set; }
}
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-alpha.11 | 51 | 7/15/2024 |
1.0.0-alpha.10 | 67 | 4/12/2024 |
1.0.0-alpha.9 | 59 | 4/12/2024 |
1.0.0-alpha.8 | 55 | 4/10/2024 |
1.0.0-alpha.7 | 57 | 4/9/2024 |
1.0.0-alpha.6 | 58 | 4/9/2024 |
1.0.0-alpha.5 | 64 | 4/7/2024 |
1.0.0-alpha.4 | 58 | 4/7/2024 |