First try with linq2db
This commit is contained in:
parent
5c86727baa
commit
552e9cc6df
205
UserService.DatabaseLayer/DataModels/UserDatabase.generated.cs
Normal file
205
UserService.DatabaseLayer/DataModels/UserDatabase.generated.cs
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
//---------------------------------------------------------------------------------------------------
|
||||||
|
// <auto-generated>
|
||||||
|
// This code was generated by T4Model template for T4 (https://github.com/linq2db/linq2db).
|
||||||
|
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
|
||||||
|
// </auto-generated>
|
||||||
|
//---------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#pragma warning disable 1591
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
using LinqToDB;
|
||||||
|
using LinqToDB.Mapping;
|
||||||
|
|
||||||
|
namespace DataModels
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Database : UserDatabase
|
||||||
|
/// Data Source : srvbo
|
||||||
|
/// Server Version : 5.5.5-10.3.22-MariaDB-1ubuntu1
|
||||||
|
/// </summary>
|
||||||
|
public partial class UserDatabaseDB : LinqToDB.Data.DataConnection
|
||||||
|
{
|
||||||
|
public ITable<Member> Members { get { return this.GetTable<Member>(); } }
|
||||||
|
public ITable<MembersMember> MembersMembers { get { return this.GetTable<MembersMember>(); } }
|
||||||
|
public ITable<Node> Nodes { get { return this.GetTable<Node>(); } }
|
||||||
|
public ITable<OrganizationUnit> OrganizationUnits { get { return this.GetTable<OrganizationUnit>(); } }
|
||||||
|
public ITable<SecurityGroup> SecurityGroups { get { return this.GetTable<SecurityGroup>(); } }
|
||||||
|
public ITable<User> Users { get { return this.GetTable<User>(); } }
|
||||||
|
|
||||||
|
public UserDatabaseDB()
|
||||||
|
{
|
||||||
|
InitDataContext();
|
||||||
|
InitMappingSchema();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserDatabaseDB(string configuration)
|
||||||
|
: base(configuration)
|
||||||
|
{
|
||||||
|
InitDataContext();
|
||||||
|
InitMappingSchema();
|
||||||
|
}
|
||||||
|
|
||||||
|
partial void InitDataContext ();
|
||||||
|
partial void InitMappingSchema();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table("Members")]
|
||||||
|
public partial class Member
|
||||||
|
{
|
||||||
|
[Column, NotNull] public int Id { get; set; } // int(11)
|
||||||
|
|
||||||
|
#region Associations
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// MembersMember_ibfk_2_BackReference
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="AttachedMemberId", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
|
||||||
|
public IEnumerable<MembersMember> MembersMemberIbfk2BackReferences { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// MembersMember_ibfk_1_BackReference
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="MemberId", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
|
||||||
|
public IEnumerable<MembersMember> MembersMemberibfks { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Members_ibfk_1
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="Id", CanBeNull=false, Relationship=Relationship.ManyToOne, KeyName="Members_ibfk_1", BackReferenceName="Membersibfks")]
|
||||||
|
public Node Node { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SecurityGroups_ibfk_1_BackReference
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="Id", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
|
||||||
|
public IEnumerable<SecurityGroup> SecurityGroupsibfks { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Users_ibfk_1_BackReference
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="Id", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
|
||||||
|
public IEnumerable<User> Usersibfks { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table("MembersMember")]
|
||||||
|
public partial class MembersMember
|
||||||
|
{
|
||||||
|
[PrimaryKey(1), NotNull] public int MemberId { get; set; } // int(11)
|
||||||
|
[PrimaryKey(2), NotNull] public int AttachedMemberId { get; set; } // int(11)
|
||||||
|
|
||||||
|
#region Associations
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// MembersMember_ibfk_2
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="AttachedMemberId", OtherKey="Id", CanBeNull=false, Relationship=Relationship.ManyToOne, KeyName="MembersMember_ibfk_2", BackReferenceName="MembersMemberIbfk2BackReferences")]
|
||||||
|
public Member AttachedMember { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// MembersMember_ibfk_1
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="MemberId", OtherKey="Id", CanBeNull=false, Relationship=Relationship.ManyToOne, KeyName="MembersMember_ibfk_1", BackReferenceName="MembersMemberibfks")]
|
||||||
|
public Member Member { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table("Nodes")]
|
||||||
|
public partial class Node
|
||||||
|
{
|
||||||
|
[PrimaryKey, Identity] public int Id { get; set; } // int(11)
|
||||||
|
|
||||||
|
#region Associations
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Members_ibfk_1_BackReference
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="Id", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
|
||||||
|
public IEnumerable<Member> Membersibfks { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OrganizationUnits_ibfk_1_BackReference
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="Id", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
|
||||||
|
public IEnumerable<OrganizationUnit> OrganizationUnitsibfks { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table("OrganizationUnits")]
|
||||||
|
public partial class OrganizationUnit
|
||||||
|
{
|
||||||
|
[Column, NotNull ] public int Id { get; set; } // int(11)
|
||||||
|
[Column, Nullable] public int? ManagerId { get; set; } // int(11)
|
||||||
|
[Column, Nullable] public int? ParentId { get; set; } // int(11)
|
||||||
|
|
||||||
|
#region Associations
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OrganizationUnits_ibfk_1
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="Id", CanBeNull=false, Relationship=Relationship.ManyToOne, KeyName="OrganizationUnits_ibfk_1", BackReferenceName="OrganizationUnitsibfks")]
|
||||||
|
public Node Node { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table("SecurityGroups")]
|
||||||
|
public partial class SecurityGroup
|
||||||
|
{
|
||||||
|
[Column, NotNull] public int Id { get; set; } // int(11)
|
||||||
|
|
||||||
|
#region Associations
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SecurityGroups_ibfk_1
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="Id", CanBeNull=false, Relationship=Relationship.ManyToOne, KeyName="SecurityGroups_ibfk_1", BackReferenceName="SecurityGroupsibfks")]
|
||||||
|
public Member Member { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table("Users")]
|
||||||
|
public partial class User
|
||||||
|
{
|
||||||
|
[Column, NotNull ] public int Id { get; set; } // int(11)
|
||||||
|
[Column, Nullable] public string FirstName { get; set; } // text
|
||||||
|
[Column, Nullable] public string LastName { get; set; } // text
|
||||||
|
[Column, NotNull ] public bool IsActive { get; set; } // bit(1)
|
||||||
|
|
||||||
|
#region Associations
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Users_ibfk_1
|
||||||
|
/// </summary>
|
||||||
|
[Association(ThisKey="Id", OtherKey="Id", CanBeNull=false, Relationship=Relationship.ManyToOne, KeyName="Users_ibfk_1", BackReferenceName="Usersibfks")]
|
||||||
|
public Member Member { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
public static partial class TableExtensions
|
||||||
|
{
|
||||||
|
public static MembersMember Find(this ITable<MembersMember> table, int MemberId, int AttachedMemberId)
|
||||||
|
{
|
||||||
|
return table.FirstOrDefault(t =>
|
||||||
|
t.MemberId == MemberId &&
|
||||||
|
t.AttachedMemberId == AttachedMemberId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Node Find(this ITable<Node> table, int Id)
|
||||||
|
{
|
||||||
|
return table.FirstOrDefault(t =>
|
||||||
|
t.Id == Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning restore 1591
|
54
UserService.DatabaseLayer/DataModels/UserDatabase.tt
Normal file
54
UserService.DatabaseLayer/DataModels/UserDatabase.tt
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<#@ template language="C#" debug="True" hostSpecific="True" #>
|
||||||
|
<#@ output extension=".generated.cs" #>
|
||||||
|
<#@ include file="$(LinqToDBT4MySqlTemplatesDirectory)LinqToDB.MySql.Tools.ttinclude" #>
|
||||||
|
<#@ include file="$(LinqToDBT4MySqlTemplatesDirectory)PluralizationService.ttinclude" #>
|
||||||
|
<# //@ include file="$(ProjectDir)LinqToDB.Templates\LinqToDB.MySql.Tools.ttinclude" #>
|
||||||
|
<# //@ include file="$(ProjectDir)LinqToDB.Templates\PluralizationService.ttinclude" #>
|
||||||
|
<#
|
||||||
|
/*
|
||||||
|
1. Create new *.tt file (e.g. MyDatabase.tt) in a folder where you would like to generate your data model
|
||||||
|
and copy content from this file to it. For example:
|
||||||
|
|
||||||
|
MyProject
|
||||||
|
DataModels
|
||||||
|
MyDatabase.tt
|
||||||
|
|
||||||
|
2. Modify the connection settings below to connect to your database.
|
||||||
|
|
||||||
|
3. Add connection string to the web/app.config file:
|
||||||
|
|
||||||
|
<connectionStrings>
|
||||||
|
<add name="MyDatabase" connectionString="Server=MyServer;Port=3306;Database=MyDatabase;Uid=root;Pwd=TestPassword;charset=utf8;" providerName="MySql.Data.MySqlClient" />
|
||||||
|
</connectionStrings>
|
||||||
|
|
||||||
|
4. To access your database use the following code:
|
||||||
|
|
||||||
|
using (var db = new MyDatabaseDB())
|
||||||
|
{
|
||||||
|
var q =
|
||||||
|
from c in db.Customers
|
||||||
|
select c;
|
||||||
|
|
||||||
|
foreach (var c in q)
|
||||||
|
Console.WriteLine(c.ContactName);
|
||||||
|
}
|
||||||
|
|
||||||
|
5. See more at https://linq2db.github.io/articles/T4.html
|
||||||
|
|
||||||
|
IMPORTANT: if running .tt file gives you error like this:
|
||||||
|
"error : Failed to resolve include text for file: C:\...\$(LinqToDBT4<SOME_DB>TemplatesDirectory)LinqToDB.<DB_NAME>.Tools.ttinclude"
|
||||||
|
check tt file properties.
|
||||||
|
Custom tool must be set to TextTemplatingFileGenerator, not TextTemplatingFilePreprocessor or any other value.
|
||||||
|
*/
|
||||||
|
|
||||||
|
NamespaceName = "DataModels";
|
||||||
|
|
||||||
|
// to configure GetSchemaOptions properties, add them here, before load metadata call
|
||||||
|
|
||||||
|
LoadMySqlMetadata("srvbo", "UserDatabase", "UserDbAdmin", "12345678");
|
||||||
|
// LoadMySqlMetadata(string connectionString);
|
||||||
|
|
||||||
|
// to adjust loaded database model before generation, add your code here, after load metadata, but before GenerateModel() call
|
||||||
|
|
||||||
|
GenerateModel();
|
||||||
|
#>
|
@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
@ -7,19 +7,42 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<None Include="DataModels\UserDatabase.generated.cs">
|
||||||
|
<DesignTime>True</DesignTime>
|
||||||
|
<AutoGen>True</AutoGen>
|
||||||
|
<DependentUpon>UserDatabase.tt</DependentUpon>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="linq2db.MySql" Version="3.1.3" />
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
|
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.8">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
||||||
</PackageReference>
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.8" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\UserService.Infrastructure\UserService.Infrastructure.csproj" />
|
<ProjectReference Include="..\UserService.Infrastructure\UserService.Infrastructure.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Update="DataModels\UserDatabase.generated.cs">
|
||||||
|
<DesignTime>True</DesignTime>
|
||||||
|
<AutoGen>True</AutoGen>
|
||||||
|
<DependentUpon>UserDatabase.tt</DependentUpon>
|
||||||
|
</Compile>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="DataModels\UserDatabase.tt">
|
||||||
|
<Generator>TextTemplatingFileGenerator</Generator>
|
||||||
|
<LastGenOutput>UserDatabase.generated.cs</LastGenOutput>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -24,7 +24,7 @@ namespace UserService.Infrastructure.DataModels
|
|||||||
public int CompareTo(Node? other)
|
public int CompareTo(Node? other)
|
||||||
{
|
{
|
||||||
if (ReferenceEquals(this, other)) return 0;
|
if (ReferenceEquals(this, other)) return 0;
|
||||||
if (ReferenceEquals(null, other)) return 1;
|
if (other is null) return 1;
|
||||||
var commonNameComparison = string.Compare(CommonName, other.CommonName, StringComparison.Ordinal);
|
var commonNameComparison = string.Compare(CommonName, other.CommonName, StringComparison.Ordinal);
|
||||||
if (commonNameComparison != 0) return commonNameComparison;
|
if (commonNameComparison != 0) return commonNameComparison;
|
||||||
return Id.CompareTo(other.Id);
|
return Id.CompareTo(other.Id);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user