-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
195 lines (169 loc) · 6.62 KB
/
Program.cs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;
using Devart.Data.Oracle;
using HibernatingRhinos.Profiler.Appender.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using SESVdh.Data.Ado;
namespace devart_efcore_value_conversion_bug_repro
{
public class MyContext : Context
{
public MyContext(string connectionString) : base(connectionString)
{
}
}
public class Program
{
static async Task Main(string[] args)
{
try
{
EntityFrameworkProfiler.Initialize();
var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
config.CodeFirstOptions.UseNonUnicodeStrings = true;
config.CodeFirstOptions.UseNonLobStrings = true;
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.development.json", optional: false, reloadOnChange: true);
var configuration = builder.Build();
EntityContext.ConnectionString = ComposeConnectionString(configuration);
using (var scope = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions {IsolationLevel = IsolationLevel.ReadCommitted},
TransactionScopeAsyncFlowOption.Enabled))
{
using (var context = new EntityContext())
{
context.Database.EnsureDeleted();
context.Database.ExecuteSqlCommand(@"
CREATE TABLE RIDER
(
ID NUMBER (19, 0) GENERATED ALWAYS AS IDENTITY NOT NULL,
MOUNT VARCHAR2 (100 CHAR) NOT NULL,
COMMENT2 CLOB
)");
context.Database.ExecuteSqlCommand(@"
CREATE TABLE SWORD
(
ID NUMBER (19, 0) GENERATED ALWAYS AS IDENTITY NOT NULL,
RIDER_ID NUMBER (19, 0) NOT NULL,
SWORD_TYPE VARCHAR2 (100 CHAR) NOT NULL
)");
var rider = new Rider(EquineBeast.Mule);
rider.Comment = string.Join("", Enumerable.Range(1, 5000).Select(_ => "a"));
context.Add(rider);
await context.SaveChangesAsync();
rider.PickUpSword(new Sword(SwordType.Katana));
rider.PickUpSword(new Sword(SwordType.Longsword));
await context.SaveChangesAsync();
}
scope.Complete();
}
using (var context = new EntityContext())
{
var parameter = EquineBeast.Mule;
var rider = context.Set<Rider>()
.Where(_ => _.Mount == parameter &&
_.Swords.Any(sword => sword.SwordType == SwordType.Longsword))
.Include(_ => _.Swords).FirstOrDefault();
//var rider = context.Set<Rider>().Where(_ => _.Mount == EquineBeast.Mule).FirstOrDefault();
}
Console.WriteLine("Finished.");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadKey();
}
private static string ComposeConnectionString(IConfiguration configuration)
{
var builder = new OracleConnectionStringBuilder
{
Server = configuration["DatabaseServer"],
UserId = configuration["UserId"],
Password = configuration["Password"],
ServiceName = configuration["ServiceName"],
Port = int.Parse(configuration["Port"]),
Direct = true,
Pooling = true,
LicenseKey = configuration["DevartLicenseKey"]
};
return builder.ToString();
}
}
public class EntityContext : DbContext
{
public static string ConnectionString;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder.UseOracle(ConnectionString);
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Rider>().ToTable("RIDER");
modelBuilder.Entity<Rider>().HasKey(_ => _.Id);
modelBuilder.Entity<Rider>().Property(_ => _.Id).HasColumnName("ID");
modelBuilder.Entity<Rider>().Property(_ => _.Mount).HasConversion<string>();
modelBuilder.Entity<Rider>().Property(_ => _.Mount).HasColumnName("MOUNT");
modelBuilder.Entity<Rider>().Property(_ => _.Comment).HasColumnName("COMMENT2");//.HasMaxLength(11000);
modelBuilder.Entity<Rider>().HasMany(_ => _.Swords).WithOne();
modelBuilder.Entity<Rider>().Metadata.FindNavigation($"{nameof(Rider.Swords)}").SetPropertyAccessMode(PropertyAccessMode.Field);
modelBuilder.Entity<Sword>().ToTable("SWORD");
modelBuilder.Entity<Sword>().HasKey(_ => _.Id);
modelBuilder.Entity<Sword>().Property(_ => _.Id).HasColumnName("ID");
modelBuilder.Entity<Sword>().Property(_ => _.SwordType).HasColumnName("SWORD_TYPE");
modelBuilder.Entity<Sword>().Property("RiderId").HasColumnName("RIDER_ID");
modelBuilder.Entity<Sword>().Property(_ => _.SwordType).HasConversion<string>();
}
}
public class Rider
{
public int Id { get; private set; }
public EquineBeast Mount { get; private set; }
public string Comment { get; set; }
private readonly List<Sword> _swords = new List<Sword>();
public IReadOnlyList<Sword> Swords => _swords.AsReadOnly();
private Rider()
{
// Required by EF Core
}
public Rider(EquineBeast mount)
{
Mount = mount;
}
public void PickUpSword(Sword sword)
{
_swords.Add(sword);
}
}
public class Sword
{
public int Id { get; private set; }
public SwordType SwordType { get; private set;}
private Sword()
{
// Required by EF Core
}
public Sword(SwordType type)
{
SwordType = type;
}
}
public enum EquineBeast
{
Donkey,
Mule,
Horse,
Unicorn
}
public enum SwordType
{
Katana,
Longsword,
Falx
}
}