This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
DbContext_EFv6.txt
109 lines (89 loc) · 2.9 KB
/
DbContext_EFv6.txt
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
100
101
102
103
104
105
106
107
108
109
// Choose one:
#define AppConfigConnectionString
//#define EmbeddedConnectionString
//#define NpgsqlConfiguration
// This file has your ConnectionString! -> "@Model.ConnectionString"
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
namespace @Model.Namespace {
@Each.EntityContainer
#if NpgsqlConfiguration
[DbConfigurationType(typeof(NpgsqlConfiguration))]
#endif
public class @Current.Name : DbContext {
#if AppConfigConnectionString
public @Current.Name(): base("[email protected]") {
}
// <connectionStrings>
// <add name="@Current.Name" connectionString="@Model.ConnectionString" providerName="@Model.ProviderName" />
// </connectionStrings>
#endif
#if EmbeddedConnectionString
public @Current.Name()
: base(DBUt.Connect(), true) {
}
static class DBUt {
internal static System.Data.Common.DbConnection Connect() {
var db = System.Data.Common.DbProviderFactories.GetFactory("@Model.ProviderName").CreateConnection();
db.ConnectionString = "@Model.ConnectionString";
return db;
}
}
#endif
#if NpgsqlConfiguration
public @Current.Name(String connectionString): base(connectionString) {
}
// using (var ctx = new @Current.Name("@Model.ConnectionString")) {
// Console.WriteLine(ctx.Table.ToArray());
// }
#endif
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
throw new UnintentionalCodeFirstException();
}
@Each.EntitySet
public virtual DbSet<@Current.EntityType.Name> @Current.Name { get; set; }
@EndEach
}
@Each.EntitySet
[Table("@Current.Ssdl.StoreEntitySet", Schema = "@Current.Ssdl.Schema")]
public class @Current.Name {
public @Current.Name() {
@Each.EntityType.NavigationProperty
@If.FromRole.Many
[email protected] = new HashSet<@Current.ToRole.Type>();
@EndIf
@EndEach
}
@Each.EntityType.Property
@If.Key.IsKey
[Key]
@EndIf
[Column("@Current.Ssdl.Name", Order = @Current.Order)]
public @Current.TypeSigned @Current.Name { get; set; }
@EndEach
@Each.EntityType.NavigationProperty
@If.FromRole.Many
public virtual ICollection<@Current.ToRole.Type> @Current.Name { get; set; }
@EndIf
@IfNot.FromRole.Many
public virtual @Current.ToRole.Type @Current.Name { get; set; }
@EndIf
@EndEach
}
@EndEach
@EndEach
#if NpgsqlConfiguration
class NpgsqlConfiguration : System.Data.Entity.DbConfiguration
{
public NpgsqlConfiguration()
{
SetProviderServices("Npgsql", Npgsql.NpgsqlServices.Instance);
SetProviderFactory("Npgsql", Npgsql.NpgsqlFactory.Instance);
SetDefaultConnectionFactory(new Npgsql.NpgsqlConnectionFactory());
}
}
#endif
}