-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrumpy.Entity.Context.tt
More file actions
99 lines (87 loc) · 3.58 KB
/
Grumpy.Entity.Context.tt
File metadata and controls
99 lines (87 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<#@ template debug="false" hostspecific="true" language="C#" #><#@
import namespace="System.Collections" #><#@
output extension=".cs" #><#@
include file="EF6.Utility.CS.ttinclude"#>//--------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------
<#
var projectDir = Host.ResolveAssemblyReference("$(ProjectDir)");
var modelFiles = Directory.EnumerateFiles(projectDir).Where(f => f.EndsWith(".edmx"));
var modelAssembly = Path.GetFileNameWithoutExtension(Host.ResolveAssemblyReference("$(TargetPath)"));
var textTransform = DynamicTextTransformation.Create(this);
var code = new CodeGenerationTools(this);
var loader = new EdmMetadataLoader(textTransform.Host, textTransform.Errors);
if (!modelFiles.Any())
return "// No Entity Framework Model (.edmx) found";
var codeNamespace = code.VsNamespaceSuggestion();
var fileManager = EntityFrameworkTemplateFileManager.Create(this);
foreach (var modelFile in modelFiles)
{
var modelName = Path.GetFileNameWithoutExtension(modelFile);
var fileName = $"{modelName}.Entity.Context.cs";
var itemCollection = loader.CreateEdmItemCollection(modelFile);
var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();
if (container != null)
{
fileManager.StartNewFile(fileName);
#>//--------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------
<#
if (!string.IsNullOrEmpty(codeNamespace))
{
#>namespace <#=code.EscapeNamespace(codeNamespace)#>
{
<#
}
#> using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Grumpy.Entity.Interfaces;
using Grumpy.Logging;
public partial class <#=container.Name#>
{
public ILogger Logger { get; } = NullLogger.Instance;
public <#=container.Name#>(ILogger logger, IEntityConnectionConfig entityConnectionConfig) : base(entityConnectionConfig.ConnectionString("<#=modelAssembly#>", "<#=modelName#>"))
{
Logger = logger;
Logger.Information("Creating Context for <#=container.Name#> {%ConnectionString}", entityConnectionConfig.ConnectionString("<#=modelAssembly#>", "<#=modelName#>"));
EntityFrameworkReferenceHack();
}
public <#=container.Name#>(IEntityConnectionConfig entityConnectionConfig) : base(entityConnectionConfig.ConnectionString("<#=modelAssembly#>", "<#=modelName#>"))
{
EntityFrameworkReferenceHack();
}
private void EntityFrameworkReferenceHack()
{
#pragma warning disable S1481
// NOTE: Using type from EntityFramework.SqlServer to ensure copy of dll to all application using this dll
// ReSharper disable once UnusedVariable
var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
#pragma warning restore S1481
}
}
<#
if (!String.IsNullOrEmpty(codeNamespace))
{
#>}
<#
}
}
}
fileManager.Process();
#>
<#+
public static void ArgumentNotNull<T>(T arg, string name) where T : class
{
if (arg == null)
{
throw new ArgumentNullException(name);
}
}
#>