|
@@ -0,0 +1,127 @@
|
|
1
|
+{#- This file can be edited -#}
|
|
2
|
+/**
|
|
3
|
+ * This is a generated file. Do not edit, changes will be overwritten.
|
|
4
|
+ */
|
|
5
|
+using System;
|
|
6
|
+using System.Collections.Generic;
|
|
7
|
+using System.ComponentModel.DataAnnotations.Schema;
|
|
8
|
+using System.Data;
|
|
9
|
+using Microsoft.EntityFrameworkCore;
|
|
10
|
+using Npgsql.EntityFrameworkCore.PostgreSQL;
|
|
11
|
+using WebApi.DataAccess.Models;
|
|
12
|
+
|
|
13
|
+namespace WebApi.DataAccess
|
|
14
|
+{
|
|
15
|
+ public partial class DatabaseContext : DbContext
|
|
16
|
+ {
|
|
17
|
+ private readonly string _connectionString;
|
|
18
|
+
|
|
19
|
+ public DatabaseContext(string connectionString)
|
|
20
|
+ {
|
|
21
|
+ _connectionString = connectionString;
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ public DatabaseContext(DbContextOptions options) : base(options)
|
|
25
|
+ {
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
29
|
+ {
|
|
30
|
+ if (_connectionString != null) {
|
|
31
|
+ optionsBuilder.UseNpgsql(_connectionString);
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
36
|
+ {
|
|
37
|
+ modelBuilder.HasPostgresExtension("uuid-ossp");
|
|
38
|
+ {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
|
|
39
|
+ modelBuilder.Entity<{{ table.getName() | escapeReservedWords }}>().ToTable("{{ table.getName() | escapeString }}");
|
|
40
|
+
|
|
41
|
+ {% for column in table.getColumns() %}{% if (column.isSelected()) %}
|
|
42
|
+ modelBuilder.Entity<{{ table.getName() | escapeReservedWords }}>()
|
|
43
|
+ .Property(e => e.{{ column.getName() | escapeReservedWords }})
|
|
44
|
+ .HasColumnName("{{ column.getName() | escapeString }}");
|
|
45
|
+ {% endif %}{% endfor %}
|
|
46
|
+
|
|
47
|
+ modelBuilder.Entity<{{ table.getName() | escapeReservedWords }}>()
|
|
48
|
+ .HasKey(c => new { {% for column in table.getPrimaryKeys() %}{% if (column.isSelected()) %}c.{{ column.getName() | escapeReservedWords }}{% if not (loop.last) %}, {% endif %}{% endif %}{% endfor %} });
|
|
49
|
+
|
|
50
|
+ {% for column in table.getColumns() %}{% if (column.isSelected()) and (column.hasDefaultValue()) %}
|
|
51
|
+ modelBuilder.Entity<{{ table.getName() | escapeReservedWords }}>()
|
|
52
|
+ .Property(e => e.{{ column.getName() | escapeReservedWords }})
|
|
53
|
+ .HasDefaultValueSql("{{ column.getDefaultValue() | escapeString }}");
|
|
54
|
+ {% endif %}{% endfor %}
|
|
55
|
+
|
|
56
|
+ {% for fk in table.getSourceForeignKeys() %}{% if (fk.isSelected()) %}
|
|
57
|
+ modelBuilder.Entity<{{ fk.getSourceTable().getName() | escapeReservedWords }}>()
|
|
58
|
+ .HasOne(e => e.{{ fk.getSourceForeignKeyName() | escapeReservedWords }})
|
|
59
|
+ .WithMany(e => e.{{ fk.getTargetForeignKeyName() | escapeReservedWords }})
|
|
60
|
+ .HasForeignKey(e => new { {% for column in fk.getSourceColumns() %}e.{{ column.getName | escapeReservedWords | escapeString }}{% if not (loop.last) %}, {% endif %}{% endfor %} })
|
|
61
|
+ .HasConstraintName("{{ fk.getName() | escapeString }}");
|
|
62
|
+ {% endif %}{% endfor %}
|
|
63
|
+
|
|
64
|
+ {% endif %}{% endfor %}
|
|
65
|
+ }
|
|
66
|
+ {% for table in dataSource.getTables() %}{% if (table.hasAny()) %}
|
|
67
|
+ public virtual DbSet<{{ table.getName() | escapeReservedWords }}> {{ table.getName() | escapeReservedWords }} { get; set; }
|
|
68
|
+ {% endif %}{% endfor %}
|
|
69
|
+
|
|
70
|
+ {% for sp in dataSource.getStoredProcedures() %}{% if (sp.isSelected()) %}
|
|
71
|
+ public static {% if (sp.getType().getLanguageTypeName() == null) %}void{% else %}{{ sp.getType().getLanguageTypeName() }}{% endif %} {{ sp.getName() | escapeReservedWords }}({% for arg in sp.getArguments() %}{{ arg.getType().getLanguageTypeName() }} {{ arg.getName() | escapeReservedWords }}{% if not (loop.last) %}, {% endif %}{% endfor %})
|
|
72
|
+ {
|
|
73
|
+ throw new NotImplementedException();
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public {% if (sp.getType().getLanguageTypeName() == null) %}void{% else %}{{ sp.getType().getLanguageTypeName() }}{% endif %} sp_{{ sp.getName() | escapeReservedWords }}({% for arg in sp.getArguments() %}{{ arg.getType().getLanguageTypeName() }} {{ arg.getName() | escapeReservedWords }}{% if not (loop.last) %}, {% endif %}{% endfor %})
|
|
77
|
+ {
|
|
78
|
+ using (var connection = Database.GetDbConnection()) {
|
|
79
|
+ connection.Open();
|
|
80
|
+
|
|
81
|
+ using (var command = connection.CreateCommand()) {
|
|
82
|
+ command.CommandType = CommandType.StoredProcedure;
|
|
83
|
+ command.CommandText = "{{ sp.getName() | escapeString }}";
|
|
84
|
+ {% for arg in sp.getArguments() %}
|
|
85
|
+ var arg_{{ arg.getName() }} = command.CreateParameter();
|
|
86
|
+ arg_{{ arg.getName() }}.ParameterName = "{{ arg.getName() | escapeString }}";
|
|
87
|
+ arg_{{ arg.getName() }}.Direction = ParameterDirection.Input;
|
|
88
|
+ arg_{{ arg.getName() }}.Value = {{ arg.getName() | escapeReservedWords }};
|
|
89
|
+ command.Parameters.Add(arg_{{ arg.getName() }});
|
|
90
|
+ {% endfor %}
|
|
91
|
+
|
|
92
|
+ using (var reader = command.ExecuteReader()) {
|
|
93
|
+ {% if (sp.getType().isSetOf()) %}
|
|
94
|
+ var value = new List<{{ sp.getType().getLanguageBaseTypeName() }}>();
|
|
95
|
+ {% else %}
|
|
96
|
+ var value = default({{ sp.getType().getLanguageTypeName() }});
|
|
97
|
+ {% endif %}
|
|
98
|
+
|
|
99
|
+ while (reader.Read()) {
|
|
100
|
+ {% if ((sp.getType().getLanguageType() != null)) %}
|
|
101
|
+ var item = new {{ sp.getType().getLanguageBaseTypeName() }}
|
|
102
|
+ {
|
|
103
|
+ {% for col in sp.getType().getLanguageType().getColumns() %}{{ col.getName() | escapeReservedWords }} = ({{ col.getType().getLanguageTypeName() }}) reader.GetValue({{ loop.index0 }}){% if not (loop.last) %},{% endif %}
|
|
104
|
+ {% endfor %}
|
|
105
|
+ };
|
|
106
|
+ {% else %}
|
|
107
|
+ var item = ({{ sp.getType().getLanguageBaseTypeName() }}) reader.GetValue(0);
|
|
108
|
+ {% endif %}
|
|
109
|
+ {% if (sp.getType().isSetOf()) %}
|
|
110
|
+ value.Add(item);
|
|
111
|
+ {% else %}
|
|
112
|
+ value = item;
|
|
113
|
+ {% endif %}
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ connection.Close();
|
|
117
|
+
|
|
118
|
+ {% if (sp.getType().getLanguageTypeName() != null) %}
|
|
119
|
+ return value;
|
|
120
|
+ {% endif %}
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ }
|
|
124
|
+ }
|
|
125
|
+ {% endif %}{% endfor %}
|
|
126
|
+ }
|
|
127
|
+}
|