RSCG – Atulin.AutoDbSet

name Atulin.AutoDbSet
nuget https://www.nuget.org/packages/Atulin.AutoDbSet/
link https://github.com/Atulin/AutoDbSet
author Angius Atulin

Generating DbSet properties for Entity Framework Core DbContext classes

 

This is how you can use Atulin.AutoDbSet .

The code that you start with is


<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>


The code that you will use is


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());



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; }
}




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);
}


 

The code that is generated is

//------------------------------------------------------------------------------
// <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   

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Atulin.AutoDbSet


Posted

in

,

by

Tags: