添加实体类

This commit is contained in:
zpc 2024-07-04 21:25:05 +08:00
parent 796bff8e49
commit 43a50e0d1b
19 changed files with 1107 additions and 1 deletions

4
.editorconfig Normal file
View File

@ -0,0 +1,4 @@
root = true
[*.cs]
charset = utf-8 #设置文件字符集为utf-8,在 Linux 系统中,通常推荐使用 UTF-8 而不是 UTF-8 with BOM。添加 BOM 可能会干扰那些不期望在文件开头出现非 ASCII 字节的软件对 UTF-8 的使用。

View File

@ -31,7 +31,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMeng.StableDiffusion.Te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TextGenerationApi", "src\2-api\TextGenerationApi\TextGenerationApi.csproj", "{A9C5939E-D239-4AB8-9380-7E76166326D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HuanMnegopneaiTest", "src\5-console\HuanMnegopneaiTest\HuanMnegopneaiTest.csproj", "{D2A74CFA-8A03-4272-89C7-C01830D183F1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HuanMnegopneaiTest", "src\5-console\HuanMnegopneaiTest\HuanMnegopneaiTest.csproj", "{D2A74CFA-8A03-4272-89C7-C01830D183F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HuanMeng.MiaoYu.Model", "src\0-core\HuanMeng.MiaoYu.Model\HuanMeng.MiaoYu.Model.csproj", "{B845C884-AD1A-4483-A2F6-B218DB14EA2D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6C59E75C-386D-4325-9752-D7DB8B158BF8}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -67,6 +74,10 @@ Global
{D2A74CFA-8A03-4272-89C7-C01830D183F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2A74CFA-8A03-4272-89C7-C01830D183F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2A74CFA-8A03-4272-89C7-C01830D183F1}.Release|Any CPU.Build.0 = Release|Any CPU
{B845C884-AD1A-4483-A2F6-B218DB14EA2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B845C884-AD1A-4483-A2F6-B218DB14EA2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B845C884-AD1A-4483-A2F6-B218DB14EA2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B845C884-AD1A-4483-A2F6-B218DB14EA2D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -86,6 +97,7 @@ Global
{054AFFF5-D25E-4EC7-A135-050EEE7F7FA0} = {8D39E84B-2810-41D7-AFE6-0A58E09E34C3}
{A9C5939E-D239-4AB8-9380-7E76166326D9} = {0C0B6EB5-E41D-46D9-9F60-90D320A2EEF3}
{D2A74CFA-8A03-4272-89C7-C01830D183F1} = {F34C5BC4-8810-4D6A-B003-AFE12D7ED4BA}
{B845C884-AD1A-4483-A2F6-B218DB14EA2D} = {DD14191F-22CE-48D8-A944-B8A41C97ACD4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4A1DC406-AFAA-4884-859C-51B9B26E37FC}

View File

@ -13,6 +13,7 @@ namespace HuanMeng.DotNetCore.Base
[Serializable]
public class BaseResponse<T> : IResponse
{
///// <summary>
///// Http状态码
///// </summary>
@ -54,6 +55,7 @@ namespace HuanMeng.DotNetCore.Base
Code = code;
Message = message;
Data = default(T);
}

View File

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.DotNetCore.MultiTenant.Contract
{
/// <summary>
/// 基本多租户接口
/// </summary>
public interface IMultiTenant
{
/// <summary>
/// 租户ID
/// </summary>
Guid TenantId { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.DotNetCore.MultiTenant.Contract
{
/// <summary>
/// 多租户DbContent接口
/// </summary>
public interface IMultiTenantDbContext
{
/// <summary>
/// 租户信息
/// </summary>
ITenantInfo TenantInfo { get; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.DotNetCore.MultiTenant.Contract
{
/// <summary>
/// 多租户实体类接口
/// </summary>
public interface IMultiTenantEntity : IMultiTenant
{
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.DotNetCore.MultiTenant.Contract
{
/// <summary>
/// 租户信息接口
/// </summary>
public interface ITenantInfo : IMultiTenant
{
string? Identifier { get; set; }
/// <summary>
/// Gets or sets a display friendly name for the tenant.
/// </summary>
string? Name { get; set; }
/// <summary>
/// 数据库连接字符串
/// </summary>
string? ConnectionString { get; set; }
}
}

View File

@ -0,0 +1,49 @@
using HuanMeng.DotNetCore.MultiTenant.Contract;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.DotNetCore.MultiTenant
{
/// <summary>
/// 基本多租户DbContext
/// </summary>
public class MultiTenantDbContext : DbContext, IMultiTenantDbContext
{
/// <summary>
/// 租户信息
/// </summary>
public ITenantInfo TenantInfo { get; set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="tenantInfo"></param>
public MultiTenantDbContext(ITenantInfo tenantInfo)
{
this.TenantInfo = tenantInfo;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="tenantInfo"></param>
/// <param name="options"></param>
public MultiTenantDbContext(ITenantInfo tenantInfo, DbContextOptions options)
: base(options)
{
this.TenantInfo = tenantInfo;
}
public void SetTenantInfo(ITenantInfo tenantInfo)
{
this.TenantInfo = tenantInfo;
}
}
}

View File

@ -0,0 +1,22 @@
using HuanMeng.DotNetCore.MultiTenant.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.DotNetCore.MultiTenant
{
/// <summary>
/// 基本多租户实体类
/// </summary>
public class MultiTenantEntity : IMultiTenantEntity
{
/// <summary>
/// 租户ID
/// </summary>
public Guid TenantId { get; set; }
}
}

View File

@ -0,0 +1,386 @@
<#@ template hostSpecific="true" #>
<#@ assembly name="Microsoft.EntityFrameworkCore" #>
<#@ assembly name="Microsoft.EntityFrameworkCore.Design" #>
<#@ assembly name="Microsoft.EntityFrameworkCore.Relational" #>
<#@ assembly name="Microsoft.Extensions.DependencyInjection.Abstractions" #>
<#@ parameter name="Model" type="Microsoft.EntityFrameworkCore.Metadata.IModel" #>
<#@ parameter name="Options" type="Microsoft.EntityFrameworkCore.Scaffolding.ModelCodeGenerationOptions" #>
<#@ parameter name="NamespaceHint" type="System.String" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="Microsoft.EntityFrameworkCore" #>
<#@ import namespace="Microsoft.EntityFrameworkCore.Design" #>
<#@ import namespace="Microsoft.EntityFrameworkCore.Infrastructure" #>
<#@ import namespace="Microsoft.EntityFrameworkCore.Scaffolding" #>
<#@ import namespace="Microsoft.Extensions.DependencyInjection" #>
<#
if (!ProductInfo.GetVersion().StartsWith("8.0"))
{
Warning("Your templates were created using an older version of Entity Framework. Additional features and bug fixes may be available. See https://aka.ms/efcore-docs-updating-templates for more information.");
}
var services = (IServiceProvider)Host;
var providerCode = services.GetRequiredService<IProviderConfigurationCodeGenerator>();
var annotationCodeGenerator = services.GetRequiredService<IAnnotationCodeGenerator>();
var code = services.GetRequiredService<ICSharpHelper>();
var usings = new List<string>
{
"System",
"System.Collections.Generic",
"Microsoft.EntityFrameworkCore"
};
if (NamespaceHint != Options.ModelNamespace
&& !string.IsNullOrEmpty(Options.ModelNamespace))
{
usings.Add(Options.ModelNamespace);
}
if (!string.IsNullOrEmpty(NamespaceHint))
{
#>
namespace <#= NamespaceHint #>;
<#
}
#>
/// <summary>
/// 妙语实体类
/// </summary>
public partial class <#= Options.ContextName #> : MultiTenantDbContext//DbContext
{
<#
if (!Options.SuppressOnConfiguring)
{
#>
public <#= Options.ContextName #>() : base(null)
{
}
<#
}
#>
public <#= Options.ContextName #>(DbContextOptions<<#= Options.ContextName #>> options)
: base(null, options)
{
}
/// <summary>
///
/// </summary>
/// <param name="tenantInfo"></param>
public <#= Options.ContextName #>(ITenantInfo tenantInfo) : base(tenantInfo)
{
}
/// <summary>
///
/// </summary>
/// <param name="tenantInfo"></param>
/// <param name="options"></param>
public <#= Options.ContextName #>(ITenantInfo tenantInfo, DbContextOptions<<#= Options.ContextName #>> options) : base(tenantInfo, options)
{
}
<#
foreach (var entityType in Model.GetEntityTypes().Where(e => !e.IsSimpleManyToManyJoinEntityType()))
{
#>
/// <summary>
/// <#=entityType.GetComment()??"" #>
/// </summary>
public virtual DbSet<<#= entityType.Name #>> <#= entityType.GetDbSetName() #> { get; set; }
<#
}
if (!Options.SuppressOnConfiguring)
{
#>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
<#
if (!Options.SuppressConnectionStringWarning)
{
#>
//#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
<#
}
var useProviderCall = providerCode.GenerateUseProvider(Options.ConnectionString);
usings.AddRange(useProviderCall.GetRequiredUsings());
#>
=> optionsBuilder<#= code.Fragment(useProviderCall, indent: 3) #>;
<#
}
#>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
<#
var anyConfiguration = false;
var modelFluentApiCalls = Model.GetFluentApiCalls(annotationCodeGenerator);
if (modelFluentApiCalls != null)
{
usings.AddRange(modelFluentApiCalls.GetRequiredUsings());
#>
modelBuilder<#= code.Fragment(modelFluentApiCalls, indent: 3) #>;
<#
anyConfiguration = true;
}
StringBuilder mainEnvironment;
foreach (var entityType in Model.GetEntityTypes().Where(e => !e.IsSimpleManyToManyJoinEntityType()))
{
// Save all previously generated code, and start generating into a new temporary environment
mainEnvironment = GenerationEnvironment;
GenerationEnvironment = new StringBuilder();
if (anyConfiguration)
{
WriteLine("");
}
var anyEntityTypeConfiguration = false;
#>
modelBuilder.Entity<<#= entityType.Name #>>(entity =>
{
<#
var key = entityType.FindPrimaryKey();
if (key != null)
{
var keyFluentApiCalls = key.GetFluentApiCalls(annotationCodeGenerator);
if (keyFluentApiCalls != null
|| (!key.IsHandledByConvention() && !Options.UseDataAnnotations))
{
if (keyFluentApiCalls != null)
{
usings.AddRange(keyFluentApiCalls.GetRequiredUsings());
}
#>
entity.HasKey(<#= code.Lambda(key.Properties, "e") #>)<#= code.Fragment(keyFluentApiCalls, indent: 4) #>;
<#
anyEntityTypeConfiguration = true;
}
}
var entityTypeFluentApiCalls = entityType.GetFluentApiCalls(annotationCodeGenerator)
?.FilterChain(c => !(Options.UseDataAnnotations && c.IsHandledByDataAnnotations));
if (entityTypeFluentApiCalls != null)
{
usings.AddRange(entityTypeFluentApiCalls.GetRequiredUsings());
if (anyEntityTypeConfiguration)
{
WriteLine("");
}
#>
entity<#= code.Fragment(entityTypeFluentApiCalls, indent: 4) #>;
<#
anyEntityTypeConfiguration = true;
}
foreach (var index in entityType.GetIndexes()
.Where(i => !(Options.UseDataAnnotations && i.IsHandledByDataAnnotations(annotationCodeGenerator))))
{
if (anyEntityTypeConfiguration)
{
WriteLine("");
}
var indexFluentApiCalls = index.GetFluentApiCalls(annotationCodeGenerator);
if (indexFluentApiCalls != null)
{
usings.AddRange(indexFluentApiCalls.GetRequiredUsings());
}
#>
entity.HasIndex(<#= code.Lambda(index.Properties, "e") #>, <#= code.Literal(index.GetDatabaseName()) #>)<#= code.Fragment(indexFluentApiCalls, indent: 4) #>;
<#
anyEntityTypeConfiguration = true;
}
var firstProperty = true;
foreach (var property in entityType.GetProperties())
{
var propertyFluentApiCalls = property.GetFluentApiCalls(annotationCodeGenerator)
?.FilterChain(c => !(Options.UseDataAnnotations && c.IsHandledByDataAnnotations)
&& !(c.Method == "IsRequired" && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType));
if (propertyFluentApiCalls == null)
{
continue;
}
usings.AddRange(propertyFluentApiCalls.GetRequiredUsings());
if (anyEntityTypeConfiguration && firstProperty)
{
WriteLine("");
}
#>
entity.Property(e => e.<#= property.Name #>)<#= code.Fragment(propertyFluentApiCalls, indent: 4) #>;
<#
anyEntityTypeConfiguration = true;
firstProperty = false;
}
foreach (var foreignKey in entityType.GetForeignKeys())
{
var foreignKeyFluentApiCalls = foreignKey.GetFluentApiCalls(annotationCodeGenerator)
?.FilterChain(c => !(Options.UseDataAnnotations && c.IsHandledByDataAnnotations));
if (foreignKeyFluentApiCalls == null)
{
continue;
}
usings.AddRange(foreignKeyFluentApiCalls.GetRequiredUsings());
if (anyEntityTypeConfiguration)
{
WriteLine("");
}
#>
entity.HasOne(d => d.<#= foreignKey.DependentToPrincipal.Name #>).<#= foreignKey.IsUnique ? "WithOne" : "WithMany" #>(<#= foreignKey.PrincipalToDependent != null ? $"p => p.{foreignKey.PrincipalToDependent.Name}" : "" #>)<#= code.Fragment(foreignKeyFluentApiCalls, indent: 4) #>;
<#
anyEntityTypeConfiguration = true;
}
foreach (var skipNavigation in entityType.GetSkipNavigations().Where(n => n.IsLeftNavigation()))
{
if (anyEntityTypeConfiguration)
{
WriteLine("");
}
var left = skipNavigation.ForeignKey;
var leftFluentApiCalls = left.GetFluentApiCalls(annotationCodeGenerator, useStrings: true);
var right = skipNavigation.Inverse.ForeignKey;
var rightFluentApiCalls = right.GetFluentApiCalls(annotationCodeGenerator, useStrings: true);
var joinEntityType = skipNavigation.JoinEntityType;
if (leftFluentApiCalls != null)
{
usings.AddRange(leftFluentApiCalls.GetRequiredUsings());
}
if (rightFluentApiCalls != null)
{
usings.AddRange(rightFluentApiCalls.GetRequiredUsings());
}
#>
entity.HasMany(d => d.<#= skipNavigation.Name #>).WithMany(p => p.<#= skipNavigation.Inverse.Name #>)
.UsingEntity<Dictionary<string, object>>(
<#= code.Literal(joinEntityType.Name) #>,
r => r.HasOne<<#= right.PrincipalEntityType.Name #>>().WithMany()<#= code.Fragment(rightFluentApiCalls, indent: 6) #>,
l => l.HasOne<<#= left.PrincipalEntityType.Name #>>().WithMany()<#= code.Fragment(leftFluentApiCalls, indent: 6) #>,
j =>
{
<#
var joinKey = joinEntityType.FindPrimaryKey();
var joinKeyFluentApiCalls = joinKey.GetFluentApiCalls(annotationCodeGenerator);
if (joinKeyFluentApiCalls != null)
{
usings.AddRange(joinKeyFluentApiCalls.GetRequiredUsings());
}
#>
j.HasKey(<#= code.Arguments(joinKey.Properties.Select(e => e.Name)) #>)<#= code.Fragment(joinKeyFluentApiCalls, indent: 7) #>;
<#
var joinEntityTypeFluentApiCalls = joinEntityType.GetFluentApiCalls(annotationCodeGenerator);
if (joinEntityTypeFluentApiCalls != null)
{
usings.AddRange(joinEntityTypeFluentApiCalls.GetRequiredUsings());
#>
j<#= code.Fragment(joinEntityTypeFluentApiCalls, indent: 7) #>;
<#
}
foreach (var index in joinEntityType.GetIndexes())
{
var indexFluentApiCalls = index.GetFluentApiCalls(annotationCodeGenerator);
if (indexFluentApiCalls != null)
{
usings.AddRange(indexFluentApiCalls.GetRequiredUsings());
}
#>
j.HasIndex(<#= code.Literal(index.Properties.Select(e => e.Name).ToArray()) #>, <#= code.Literal(index.GetDatabaseName()) #>)<#= code.Fragment(indexFluentApiCalls, indent: 7) #>;
<#
}
foreach (var property in joinEntityType.GetProperties())
{
var propertyFluentApiCalls = property.GetFluentApiCalls(annotationCodeGenerator);
if (propertyFluentApiCalls == null)
{
continue;
}
usings.AddRange(propertyFluentApiCalls.GetRequiredUsings());
#>
j.IndexerProperty<<#= code.Reference(property.ClrType) #>>(<#= code.Literal(property.Name) #>)<#= code.Fragment(propertyFluentApiCalls, indent: 7) #>;
//添加全局筛选器
if (this.TenantInfo != null)
{
entity.HasQueryFilter(it => it.TenantId == this.TenantInfo.TenantId);
}
<#
}
#>
});
<#
anyEntityTypeConfiguration = true;
}
#>
});
<#
// If any signicant code was generated, append it to the main environment
if (anyEntityTypeConfiguration)
{
mainEnvironment.Append(GenerationEnvironment);
anyConfiguration = true;
}
// Resume generating code into the main environment
GenerationEnvironment = mainEnvironment;
}
foreach (var sequence in Model.GetSequences())
{
var needsType = sequence.Type != typeof(long);
var needsSchema = !string.IsNullOrEmpty(sequence.Schema) && sequence.Schema != sequence.Model.GetDefaultSchema();
var sequenceFluentApiCalls = sequence.GetFluentApiCalls(annotationCodeGenerator);
#>
modelBuilder.HasSequence<#= needsType ? $"<{code.Reference(sequence.Type)}>" : "" #>(<#= code.Literal(sequence.Name) #><#= needsSchema ? $", {code.Literal(sequence.Schema)}" : "" #>)<#= code.Fragment(sequenceFluentApiCalls, indent: 3) #>;
<#
}
if (anyConfiguration)
{
WriteLine("");
}
#>
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
<#
mainEnvironment = GenerationEnvironment;
GenerationEnvironment = new StringBuilder();
foreach (var ns in usings.Distinct().OrderBy(x => x, new NamespaceComparer()))
{
#>
using <#= ns #>;
<#
}
WriteLine("");
GenerationEnvironment.Append(mainEnvironment);
#>

View File

@ -0,0 +1,177 @@
<#@ template hostSpecific="true" #>
<#@ assembly name="Microsoft.EntityFrameworkCore" #>
<#@ assembly name="Microsoft.EntityFrameworkCore.Design" #>
<#@ assembly name="Microsoft.EntityFrameworkCore.Relational" #>
<#@ assembly name="Microsoft.Extensions.DependencyInjection.Abstractions" #>
<#@ parameter name="EntityType" type="Microsoft.EntityFrameworkCore.Metadata.IEntityType" #>
<#@ parameter name="Options" type="Microsoft.EntityFrameworkCore.Scaffolding.ModelCodeGenerationOptions" #>
<#@ parameter name="NamespaceHint" type="System.String" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.ComponentModel.DataAnnotations" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="Microsoft.EntityFrameworkCore" #>
<#@ import namespace="Microsoft.EntityFrameworkCore.Design" #>
<#@ import namespace="Microsoft.Extensions.DependencyInjection" #>
<#
if (EntityType.IsSimpleManyToManyJoinEntityType())
{
// Don't scaffold these
return "";
}
var services = (IServiceProvider)Host;
var annotationCodeGenerator = services.GetRequiredService<IAnnotationCodeGenerator>();
var code = services.GetRequiredService<ICSharpHelper>();
var usings = new List<string>
{
// "System",
//"System.Collections.Generic",
};
if (Options.UseDataAnnotations)
{
usings.Add("System.ComponentModel.DataAnnotations");
usings.Add("System.ComponentModel.DataAnnotations.Schema");
usings.Add("Microsoft.EntityFrameworkCore");
}
if (!string.IsNullOrEmpty(NamespaceHint))
{
#>
namespace <#= NamespaceHint #>;
<#
}
if (!string.IsNullOrEmpty(EntityType.GetComment()))
{
#>
/// <summary>
/// <#= code.XmlComment(EntityType.GetComment()) #>
/// </summary>
<#
}
if (Options.UseDataAnnotations)
{
foreach (var dataAnnotation in EntityType.GetDataAnnotations(annotationCodeGenerator))
{
#>
<#= code.Fragment(dataAnnotation) #>
<#
}
}
#>
public partial class <#= EntityType.Name #>: MultiTenantEntity
{
<#
var firstProperty = true;
foreach (var property in EntityType.GetProperties().OrderBy(p => p.GetColumnOrder() ?? -1))
{
if (!firstProperty)
{
WriteLine("");
}
if(property.Name=="TenantId"){
continue;
}
if (!string.IsNullOrEmpty(property.GetComment()))
{
#>
/// <summary>
/// <#= code.XmlComment(property.GetComment(), indent: 1) #>
/// </summary>
<#
}
if (Options.UseDataAnnotations)
{
var dataAnnotations = property.GetDataAnnotations(annotationCodeGenerator)
.Where(a => !(a.Type == typeof(RequiredAttribute) && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType));
foreach (var dataAnnotation in dataAnnotations)
{
#>
<#= code.Fragment(dataAnnotation) #>
<#
}
}
usings.AddRange(code.GetRequiredUsings(property.ClrType));
var needsNullable = Options.UseNullableReferenceTypes && property.IsNullable && !property.ClrType.IsValueType;
var needsInitializer = Options.UseNullableReferenceTypes && !property.IsNullable && !property.ClrType.IsValueType;
#>
public <#= code.Reference(property.ClrType) #><#= needsNullable ? "?" : "" #> <#= property.Name #> { get; set; }<#= needsInitializer ? " = null!;" : "" #>
<#
firstProperty = false;
}
foreach (var navigation in EntityType.GetNavigations())
{
WriteLine("");
if (Options.UseDataAnnotations)
{
foreach (var dataAnnotation in navigation.GetDataAnnotations(annotationCodeGenerator))
{
#>
<#= code.Fragment(dataAnnotation) #>
<#
}
}
var targetType = navigation.TargetEntityType.Name;
if (navigation.IsCollection)
{
#>
public virtual ICollection<<#= targetType #>> <#= navigation.Name #> { get; set; } = new List<<#= targetType #>>();
<#
}
else
{
var needsNullable = Options.UseNullableReferenceTypes && !(navigation.ForeignKey.IsRequired && navigation.IsOnDependent);
var needsInitializer = Options.UseNullableReferenceTypes && navigation.ForeignKey.IsRequired && navigation.IsOnDependent;
#>
public virtual <#= targetType #><#= needsNullable ? "?" : "" #> <#= navigation.Name #> { get; set; }<#= needsInitializer ? " = null!;" : "" #>
<#
}
}
foreach (var skipNavigation in EntityType.GetSkipNavigations())
{
WriteLine("");
if (Options.UseDataAnnotations)
{
foreach (var dataAnnotation in skipNavigation.GetDataAnnotations(annotationCodeGenerator))
{
#>
<#= code.Fragment(dataAnnotation) #>
<#
}
}
#>
public virtual ICollection<<#= skipNavigation.TargetEntityType.Name #>> <#= skipNavigation.Name #> { get; set; } = new List<<#= skipNavigation.TargetEntityType.Name #>>();
<#
}
#>
}
<#
var previousOutput = GenerationEnvironment;
GenerationEnvironment = new StringBuilder();
foreach (var ns in usings.Distinct().OrderBy(x => x, new NamespaceComparer()))
{
#>
using <#= ns #>;
<#
}
WriteLine("");
GenerationEnvironment.Append(previousOutput);
#>

View File

@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
/// <summary>
///
/// </summary>
public partial class MiaoYuContext : MultiTenantDbContext//DbContext
{
public MiaoYuContext() : base(null)
{
}
public MiaoYuContext(DbContextOptions<MiaoYuContext> options)
: base(null, options)
{
}
/// <summary>
///
/// </summary>
/// <param name="tenantInfo"></param>
public MiaoYuContext(ITenantInfo tenantInfo) : base(tenantInfo)
{
}
/// <summary>
///
/// </summary>
/// <param name="tenantInfo"></param>
/// <param name="options"></param>
public MiaoYuContext(ITenantInfo tenantInfo, DbContextOptions<MiaoYuContext> options) : base(tenantInfo, options)
{
}
/// <summary>
/// 用户表
/// </summary>
public virtual DbSet<T_User> T_User { get; set; }
/// <summary>
/// 用户信息表
/// </summary>
public virtual DbSet<T_User_Data> T_User_Data { get; set; }
/// <summary>
/// 手机号登录表
/// </summary>
public virtual DbSet<T_User_Phone_Account> T_User_Phone_Account { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlServer("Server=192.168.195.2;Database=MiaoYu;User Id=zpc;Password=zpc;TrustServerCertificate=true;");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<T_User>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__T_User__3214EC073733108B");
entity.ToTable(tb => tb.HasComment("用户表"));
entity.Property(e => e.Id).HasComment("用户Id");
entity.Property(e => e.CreatedAt)
.HasComment("创建时间")
.HasColumnType("datetime");
entity.Property(e => e.Email)
.HasMaxLength(255)
.HasComment("绑定的邮箱");
entity.Property(e => e.IsActive).HasComment("是否活跃");
entity.Property(e => e.LastLoginAt)
.HasComment("最后一次登录时间")
.HasColumnType("datetime");
entity.Property(e => e.LastLoginTypeAt).HasComment("最后一次登录方式,1手机号");
entity.Property(e => e.NickName)
.HasMaxLength(100)
.HasComment("用户昵称");
entity.Property(e => e.PhoneNum)
.HasMaxLength(50)
.IsUnicode(false)
.HasComment("绑定的手机号");
entity.Property(e => e.RegisterType).HasComment("首次注册方式");
entity.Property(e => e.TenantId).HasComment("租户Id");
entity.Property(e => e.UpdatedAt)
.HasComment("修改时间")
.HasColumnType("datetime");
entity.Property(e => e.UserName)
.HasMaxLength(100)
.HasComment("用户姓名");
});
modelBuilder.Entity<T_User_Data>(entity =>
{
entity.HasKey(e => new { e.Id, e.UserId }).HasName("PK__T_User_D__E36C60C3D959FD89");
entity.ToTable(tb => tb.HasComment("用户信息表"));
entity.Property(e => e.Id)
.ValueGeneratedOnAdd()
.HasComment("主键");
entity.Property(e => e.UserId).HasComment("用户Id");
entity.Property(e => e.CreatedAt_)
.HasComment("创建时间")
.HasColumnType("datetime")
.HasColumnName("CreatedAt ");
entity.Property(e => e.Currency).HasComment("货币");
entity.Property(e => e.IP)
.HasMaxLength(25)
.IsUnicode(false)
.HasComment("Ip");
entity.Property(e => e.NickName)
.HasMaxLength(50)
.HasComment("用户昵称,需要和主表保持一致");
entity.Property(e => e.UpdatedAt)
.HasComment("更新时间")
.HasColumnType("datetime");
entity.Property(e => e.UserIconUrl)
.HasMaxLength(300)
.HasComment("用户头像");
entity.Property(e => e.VipType).HasComment("vip类型");
});
modelBuilder.Entity<T_User_Phone_Account>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__T_User_P__3214EC07987BDDB2");
entity.ToTable(tb => tb.HasComment("手机号登录表"));
entity.Property(e => e.Id).HasComment("主键");
entity.Property(e => e.CreatedAt)
.HasComment("修改时间")
.HasColumnType("datetime");
entity.Property(e => e.LastLoginAt)
.HasComment("最后一次登录时间")
.HasColumnType("datetime");
entity.Property(e => e.NikeName)
.HasMaxLength(1)
.HasComment("用户昵称");
entity.Property(e => e.PhoneNum)
.HasMaxLength(50)
.IsUnicode(false)
.HasComment("手机号");
entity.Property(e => e.TenantId).HasComment("租户Id");
entity.Property(e => e.UpdatedAt)
.HasComment("创建时间")
.HasColumnType("datetime");
entity.Property(e => e.UserId).HasComment("用户Id");
entity.Property(e => e.VerificationCode)
.HasMaxLength(10)
.IsUnicode(false)
.HasComment("验证码");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

View File

@ -0,0 +1,65 @@
using System;
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
/// <summary>
/// 用户表
/// </summary>
public partial class T_User: MultiTenantEntity
{
/// <summary>
/// 用户Id
/// </summary>
public int Id { get; set; }
/// <summary>
/// 用户昵称
/// </summary>
public string NickName { get; set; } = null!;
/// <summary>
/// 用户姓名
/// </summary>
public string? UserName { get; set; }
/// <summary>
/// 绑定的手机号
/// </summary>
public string? PhoneNum { get; set; }
/// <summary>
/// 绑定的邮箱
/// </summary>
public string? Email { get; set; }
/// <summary>
/// 是否活跃
/// </summary>
public bool IsActive { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreatedAt { get; set; }
/// <summary>
/// 最后一次登录方式,1手机号
/// </summary>
public int LastLoginTypeAt { get; set; }
/// <summary>
/// 最后一次登录时间
/// </summary>
public DateTime LastLoginAt { get; set; }
/// <summary>
/// 修改时间
/// </summary>
public DateTime UpdatedAt { get; set; }
/// <summary>
/// 首次注册方式
/// </summary>
public int RegisterType { get; set; }
}

View File

@ -0,0 +1,55 @@
using System;
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
/// <summary>
/// 用户信息表
/// </summary>
public partial class T_User_Data: MultiTenantEntity
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 用户Id
/// </summary>
public int UserId { get; set; }
/// <summary>
/// 用户昵称,需要和主表保持一致
/// </summary>
public string? NickName { get; set; }
/// <summary>
/// vip类型
/// </summary>
public int VipType { get; set; }
/// <summary>
/// 货币
/// </summary>
public int Currency { get; set; }
/// <summary>
/// 用户头像
/// </summary>
public string? UserIconUrl { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreatedAt_ { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdatedAt { get; set; }
/// <summary>
/// Ip
/// </summary>
public string? IP { get; set; }
}

View File

@ -0,0 +1,50 @@
using System;
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
/// <summary>
/// 手机号登录表
/// </summary>
public partial class T_User_Phone_Account: MultiTenantEntity
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 用户Id
/// </summary>
public int UserId { get; set; }
/// <summary>
/// 手机号
/// </summary>
public string PhoneNum { get; set; } = null!;
/// <summary>
/// 验证码
/// </summary>
public string VerificationCode { get; set; } = null!;
/// <summary>
/// 最后一次登录时间
/// </summary>
public DateTime LastLoginAt { get; set; }
/// <summary>
/// 修改时间
/// </summary>
public DateTime CreatedAt { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime UpdatedAt { get; set; }
/// <summary>
/// 用户昵称
/// </summary>
public string? NikeName { get; set; }
}

View File

@ -0,0 +1,5 @@

##在API项目里面执行使用连接字符串名称生成代码到model项目
```sh
dotnet ef dbcontext scaffold "Server=192.168.195.2;Database=MiaoYu;User Id=zpc;Password=zpc;TrustServerCertificate=true;" Microsoft.EntityFrameworkCore.SqlServer -o DbSqlServer/Db_MiaoYu/ --use-database-names --no-pluralize --force
```

View File

@ -0,0 +1,2 @@
global using HuanMeng.DotNetCore.MultiTenant;
global using HuanMeng.DotNetCore.MultiTenant.Contract;

View File

@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HuanMeng.DotNetCore\HuanMeng.DotNetCore.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="DbSqlServer\Db_MiaoYu\" />
</ItemGroup>
</Project>