|
1 | | -using Microsoft.EntityFrameworkCore; |
2 | | -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
3 | | - |
4 | | -using SmartEnum.EFCore; |
5 | | - |
6 | | -namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext; |
7 | | - |
8 | | -public class TestDbContext : TestDbContextBase |
9 | | -{ |
10 | | - public DbSet<TestEntity> TestEntities { get; set; } = null!; |
11 | | - public DbSet<TestEntityWithSimpleTypes> TestEntitiesWithSimpleTypes { get; set; } = null!; |
12 | | - public DbSet<TestEntityWithJson> TestEntitiesWithJson { get; set; } = null!; |
13 | | - public DbSet<TestEntityWithGuidId> TestEntitiesWithGuidId { get; set; } = null!; |
14 | | - public DbSet<TestEntityWithConverters> TestEntitiesWithConverter { get; set; } = null!; |
15 | | - public DbSet<TestEntityWithComplexType> TestEntitiesWithComplexType { get; set; } = null!; |
16 | | - public DbSet<TestEntityWithSmartEnum> TestEntitiesWithSmartEnum { get; set; } = null!; |
17 | | - public DbSet<Student> Students { get; set; } = null!; |
18 | | - public DbSet<Course> Courses { get; set; } = null!; |
19 | | - |
20 | | - protected override void OnModelCreating(ModelBuilder modelBuilder) |
21 | | - { |
22 | | - base.OnModelCreating(modelBuilder); |
23 | | - |
24 | | - modelBuilder.ConfigureSmartEnum(); |
25 | | - |
26 | | - modelBuilder.Entity<TestEntityWithConverters>(builder => |
27 | | - { |
28 | | - builder.Property(e => e.CreatedAt) |
29 | | - .HasConversion(new DateTimeToBinaryConverter()); |
30 | | - }); |
31 | | - |
32 | | - modelBuilder.Entity<TestEntityWithGuidId>(builder => |
33 | | - { |
34 | | - builder.Property(e => e.Id) |
35 | | - .ValueGeneratedNever(); |
36 | | - }); |
37 | | - |
38 | | - modelBuilder.Entity<TestEntityWithComplexType>(builder => |
39 | | - { |
40 | | - builder |
41 | | - .ComplexProperty(e => e.OwnedComplexType) |
42 | | - .IsRequired(); |
43 | | - }); |
44 | | - |
45 | | - // Many-to-many with shadow property |
46 | | - modelBuilder.Entity<Student>() |
47 | | - .HasMany(s => s.Courses) |
48 | | - .WithMany(c => c.Students) |
49 | | - .UsingEntity<Dictionary<string, object>>( |
50 | | - "StudentCourse", |
51 | | - j => j.HasOne<Course>().WithMany().HasForeignKey("CourseId"), |
52 | | - j => j.HasOne<Student>().WithMany().HasForeignKey("StudentId"), |
53 | | - j => |
54 | | - { |
55 | | - j.Property<DateTime>("EnrolledAt"); |
56 | | - j.HasKey("StudentId", "CourseId"); |
57 | | - } |
58 | | - ); |
59 | | - } |
60 | | -} |
61 | | - |
62 | | -public class TestDbContextPostgreSql : TestDbContext |
63 | | -{ |
64 | | - protected override void OnModelCreating(ModelBuilder modelBuilder) |
65 | | - { |
66 | | - base.OnModelCreating(modelBuilder); |
67 | | - |
68 | | - modelBuilder.Entity<TestEntityWithJson>(b => |
69 | | - { |
70 | | - b.Property(x => x.JsonArray).AsJsonString("jsonb"); |
71 | | - b.Property(x => x.JsonObject).AsJsonString("jsonb"); |
72 | | - }); |
73 | | - |
74 | | - modelBuilder.Entity<TestEntity>(b => |
75 | | - { |
76 | | - b.Property(x => x.StringEnumValue).HasColumnType("text"); |
77 | | - }); |
78 | | - } |
79 | | -} |
80 | | - |
81 | | -public class TestDbContextMySql : TestDbContext |
82 | | -{ |
83 | | - protected override void OnModelCreating(ModelBuilder modelBuilder) |
84 | | - { |
85 | | - base.OnModelCreating(modelBuilder); |
86 | | - |
87 | | - modelBuilder.Entity<TestEntityWithJson>(b => |
88 | | - { |
89 | | - b.Property(x => x.JsonArray).AsJsonString("json"); |
90 | | - b.Property(x => x.JsonObject).AsJsonString("json"); |
91 | | - }); |
92 | | - |
93 | | - modelBuilder.Entity<TestEntity>(b => |
94 | | - { |
95 | | - b.Property(x => x.StringEnumValue).HasColumnType("text"); |
96 | | - }); |
97 | | - } |
98 | | -} |
99 | | - |
100 | | -public class TestDbContextSqlServer : TestDbContext |
101 | | -{ |
102 | | - protected override void OnModelCreating(ModelBuilder modelBuilder) |
103 | | - { |
104 | | - base.OnModelCreating(modelBuilder); |
105 | | - |
106 | | - modelBuilder.Entity<TestEntityWithJson>(b => |
107 | | - { |
108 | | - b.Property(x => x.JsonArray).AsJsonString(null); |
109 | | - b.Property(x => x.JsonObject).AsJsonString(null); |
110 | | - }); |
111 | | - |
112 | | - modelBuilder.Entity<TestEntity>(b => |
113 | | - { |
114 | | - b.Property(x => x.StringEnumValue).HasColumnType("text"); |
115 | | - }); |
116 | | - } |
117 | | -} |
118 | | - |
119 | | -public class TestDbContextSqlite : TestDbContext |
120 | | -{ |
121 | | - protected override void OnModelCreating(ModelBuilder modelBuilder) |
122 | | - { |
123 | | - base.OnModelCreating(modelBuilder); |
124 | | - |
125 | | - modelBuilder.Entity<TestEntityWithJson>(b => |
126 | | - { |
127 | | - b.Property(x => x.JsonArray).AsJsonString(null); |
128 | | - b.Property(x => x.JsonObject).AsJsonString(null); |
129 | | - }); |
130 | | - |
131 | | - modelBuilder.Entity<TestEntity>(b => |
132 | | - { |
133 | | - b.Property(x => x.StringEnumValue).HasColumnType("text"); |
134 | | - }); |
135 | | - } |
136 | | -} |
137 | | - |
138 | | -public class TestDbContextOracle : TestDbContext |
139 | | -{ |
140 | | - protected override void OnModelCreating(ModelBuilder modelBuilder) |
141 | | - { |
142 | | - base.OnModelCreating(modelBuilder); |
143 | | - |
144 | | - modelBuilder.Entity<TestEntityWithJson>(b => |
145 | | - { |
146 | | - b.Property(x => x.JsonArray).AsJsonString(null); |
147 | | - b.Property(x => x.JsonObject).AsJsonString(null); |
148 | | - }); |
149 | | - |
150 | | - modelBuilder.Entity<TestEntity>(b => |
151 | | - { |
152 | | - b.Property(x => x.StringEnumValue).HasColumnType("nvarchar2(255)"); |
153 | | - }); |
154 | | - } |
155 | | -} |
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
| 3 | + |
| 4 | +using SmartEnum.EFCore; |
| 5 | + |
| 6 | +namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext; |
| 7 | + |
| 8 | +public class TestDbContext : TestDbContextBase |
| 9 | +{ |
| 10 | + public DbSet<TestEntity> TestEntities { get; set; } = null!; |
| 11 | + public DbSet<TestEntityWithSimpleTypes> TestEntitiesWithSimpleTypes { get; set; } = null!; |
| 12 | + public DbSet<TestEntityWithJson> TestEntitiesWithJson { get; set; } = null!; |
| 13 | + public DbSet<TestEntityWithGuidId> TestEntitiesWithGuidId { get; set; } = null!; |
| 14 | + public DbSet<TestEntityWithConverters> TestEntitiesWithConverter { get; set; } = null!; |
| 15 | + public DbSet<TestEntityWithComplexType> TestEntitiesWithComplexType { get; set; } = null!; |
| 16 | + public DbSet<TestEntityWithSmartEnum> TestEntitiesWithSmartEnum { get; set; } = null!; |
| 17 | + public DbSet<Student> Students { get; set; } = null!; |
| 18 | + public DbSet<Course> Courses { get; set; } = null!; |
| 19 | + |
| 20 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 21 | + { |
| 22 | + base.OnModelCreating(modelBuilder); |
| 23 | + |
| 24 | + modelBuilder.ConfigureSmartEnum(); |
| 25 | + |
| 26 | + modelBuilder.Entity<TestEntityWithConverters>(builder => |
| 27 | + { |
| 28 | + builder.Property(e => e.CreatedAt) |
| 29 | + .HasConversion(new DateTimeToBinaryConverter()); |
| 30 | + }); |
| 31 | + |
| 32 | + modelBuilder.Entity<TestEntityWithGuidId>(builder => |
| 33 | + { |
| 34 | + builder.Property(e => e.Id) |
| 35 | + .ValueGeneratedNever(); |
| 36 | + }); |
| 37 | + |
| 38 | + modelBuilder.Entity<TestEntityWithComplexType>(builder => |
| 39 | + { |
| 40 | + builder |
| 41 | + .ComplexProperty(e => e.OwnedComplexType) |
| 42 | + .IsRequired(); |
| 43 | + }); |
| 44 | + |
| 45 | + // Many-to-many with shadow property |
| 46 | + modelBuilder.Entity<Student>() |
| 47 | + .HasMany(s => s.Courses) |
| 48 | + .WithMany(c => c.Students) |
| 49 | + .UsingEntity<Dictionary<string, object>>( |
| 50 | + "StudentCourse", |
| 51 | + j => j.HasOne<Course>().WithMany().HasForeignKey("CourseId"), |
| 52 | + j => j.HasOne<Student>().WithMany().HasForeignKey("StudentId"), |
| 53 | + j => |
| 54 | + { |
| 55 | + j.Property<DateTime>("EnrolledAt"); |
| 56 | + j.HasKey("StudentId", "CourseId"); |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + // Keyless entity type |
| 61 | + modelBuilder.Entity<TestEntityKeyless>(builder => |
| 62 | + { |
| 63 | + builder.HasNoKey(); |
| 64 | + // ToView will use the given table name read-only, it doesn't have to actually be a database view. |
| 65 | + // We just reuse the table for the standard TestEntity. |
| 66 | + builder.ToView("test_entity"); |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +public class TestDbContextPostgreSql : TestDbContext |
| 72 | +{ |
| 73 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 74 | + { |
| 75 | + base.OnModelCreating(modelBuilder); |
| 76 | + |
| 77 | + modelBuilder.Entity<TestEntityWithJson>(b => |
| 78 | + { |
| 79 | + b.Property(x => x.JsonArray).AsJsonString("jsonb"); |
| 80 | + b.Property(x => x.JsonObject).AsJsonString("jsonb"); |
| 81 | + }); |
| 82 | + |
| 83 | + modelBuilder.Entity<TestEntity>(b => |
| 84 | + { |
| 85 | + b.Property(x => x.StringEnumValue).HasColumnType("text"); |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +public class TestDbContextMySql : TestDbContext |
| 91 | +{ |
| 92 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 93 | + { |
| 94 | + base.OnModelCreating(modelBuilder); |
| 95 | + |
| 96 | + modelBuilder.Entity<TestEntityWithJson>(b => |
| 97 | + { |
| 98 | + b.Property(x => x.JsonArray).AsJsonString("json"); |
| 99 | + b.Property(x => x.JsonObject).AsJsonString("json"); |
| 100 | + }); |
| 101 | + |
| 102 | + modelBuilder.Entity<TestEntity>(b => |
| 103 | + { |
| 104 | + b.Property(x => x.StringEnumValue).HasColumnType("text"); |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +public class TestDbContextSqlServer : TestDbContext |
| 110 | +{ |
| 111 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 112 | + { |
| 113 | + base.OnModelCreating(modelBuilder); |
| 114 | + |
| 115 | + modelBuilder.Entity<TestEntityWithJson>(b => |
| 116 | + { |
| 117 | + b.Property(x => x.JsonArray).AsJsonString(null); |
| 118 | + b.Property(x => x.JsonObject).AsJsonString(null); |
| 119 | + }); |
| 120 | + |
| 121 | + modelBuilder.Entity<TestEntity>(b => |
| 122 | + { |
| 123 | + b.Property(x => x.StringEnumValue).HasColumnType("text"); |
| 124 | + }); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +public class TestDbContextSqlite : TestDbContext |
| 129 | +{ |
| 130 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 131 | + { |
| 132 | + base.OnModelCreating(modelBuilder); |
| 133 | + |
| 134 | + modelBuilder.Entity<TestEntityWithJson>(b => |
| 135 | + { |
| 136 | + b.Property(x => x.JsonArray).AsJsonString(null); |
| 137 | + b.Property(x => x.JsonObject).AsJsonString(null); |
| 138 | + }); |
| 139 | + |
| 140 | + modelBuilder.Entity<TestEntity>(b => |
| 141 | + { |
| 142 | + b.Property(x => x.StringEnumValue).HasColumnType("text"); |
| 143 | + }); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +public class TestDbContextOracle : TestDbContext |
| 148 | +{ |
| 149 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 150 | + { |
| 151 | + base.OnModelCreating(modelBuilder); |
| 152 | + |
| 153 | + modelBuilder.Entity<TestEntityWithJson>(b => |
| 154 | + { |
| 155 | + b.Property(x => x.JsonArray).AsJsonString(null); |
| 156 | + b.Property(x => x.JsonObject).AsJsonString(null); |
| 157 | + }); |
| 158 | + |
| 159 | + modelBuilder.Entity<TestEntity>(b => |
| 160 | + { |
| 161 | + b.Property(x => x.StringEnumValue).HasColumnType("nvarchar2(255)"); |
| 162 | + }); |
| 163 | + } |
| 164 | +} |
0 commit comments