Mvp24Hours.Entity.SqlServer
3.7.151
dotnet add package Mvp24Hours.Entity.SqlServer --version 3.7.151
NuGet\Install-Package Mvp24Hours.Entity.SqlServer -Version 3.7.151
<PackageReference Include="Mvp24Hours.Entity.SqlServer" Version="3.7.151" />
paket add Mvp24Hours.Entity.SqlServer --version 3.7.151
#r "nuget: Mvp24Hours.Entity.SqlServer, 3.7.151"
// Install Mvp24Hours.Entity.SqlServer as a Cake Addin #addin nuget:?package=Mvp24Hours.Entity.SqlServer&version=3.7.151 // Install Mvp24Hours.Entity.SqlServer as a Cake Tool #tool nuget:?package=Mvp24Hours.Entity.SqlServer&version=3.7.151
mvp24hours-entity-t4
How to use the Mvp24Hours.Entity.SqlServer and Mono.TextTemplating library to generate templates with T4.
Tools
Run the command below to install the T4 template engine:
dotnet tool install -g dotnet-t4
How to configure?
Add the configuration below to the .csproj file
<ItemGroup>
<PackageReference Include="Mono.TextTemplating" Version="2.0.5" />
<PackageReference Include="Mvp24Hours.Entity.SqlServer" Version="1.0.0" />
<TextTemplate Include="**\*.tt" />
<Generated Include="**\*.Generated.cs" />
</ItemGroup>
<Target Name="TextTemplateTransform" BeforeTargets="BeforeBuild">
<Exec WorkingDirectory="$(ProjectDir)" Command="t4 %(TextTemplate.Identity) -c $(RootNameSpace).%(TextTemplate.Filename) -o %(TextTemplate.Filename).Generated.cs" />
</Target>
<Target Name="TextTemplateClean" AfterTargets="Clean">
<Delete Files="@(Generated)" />
</Target>
Create a file for the ITextTemplate interface:
public interface ITextTemplate
{
string TransformText() => throw new NotImplementedException();
}
Create the file that will represent the entity model or models loaded for use in ".tt", for example (ModelTemplate.cs):
public partial class ModelTemplate : ITextTemplate
{
public readonly ResultSettings Models;
public readonly EntitySettings Entity;
public ModelTemplate(ResultSettings models, EntitySettings entity)
=> (Models, Entity) = (models, entity);
}
Create a file with the extension ".tt" with the same name as the previous class, for example (ModelTemplate.tt):
<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#=Entity.Name#>
The same properties of your "ModelTemplate" class can be used in your "ModelTemplate.tt" template.
Ready! Now just run your "Program.cs" with combining the template with the models loaded from the database:
var mvpSettings = new Mvp24Settings()
{
Database = new DatabaseSettings()
{
DataSource = "",
Catalog = "",
TrustedConnection = true
},
IdKeyName = true, // Use if the columns that represent your table's primary key have the name "ID"
TableFilter = "", // Use to filter the tables you want to read
};
var dbEntity = new DbEntitySqlServer(mvpSettings);
var settings = dbEntity.GetSettings();
if (settings != null)
{
System.Console.WriteLine("Writing processed results in the \"Models\" folder..");
string pathDir = ""; // What is the output directory of the processed files?
foreach (var entity in settings.Entities)
{
System.Console.WriteLine($"Writing {entity.ClassName}.cs");
ITextTemplate template = new ModelTemplate(settings, entity);
File.WriteAllText($"{pathDir}{entity.ClassName}.cs", template.TransformText());
}
}
When the project is compiled, a "Generated" file (ModelTemplate.Generated.cs) will be generated that corresponds to the processed template.
How to Configure Using a JSON File?
If you want to use a configuration file, create a ".json" file with the following structure:
{
"database": {
"datasource": "",
"catalog": "",
"trustedconnection": "true",
"username": "",
"password": ""
},
"generate": true,
"schemafilter": "",
"tablefilter": "",
"idkeyname": false
}
Just inform the name of the file that is in the same directory when creating the "DbEntitySqlServer" object, like this:
var dbEntity = new DbEntitySqlServer("mvp24hours-entity-settings.json");
var settings = dbEntity.GetSettings();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Mvp24Hours.Entity.Core (>= 3.7.151)
- System.Data.SqlClient (>= 4.8.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.