This repository was archived by the owner on Feb 1, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 8 files changed +91
-9
lines changed
Source/LinqToDB.EntityFrameworkCore
LinqToDB.EntityFrameworkCore.BaseTests
LinqToDB.EntityFrameworkCore.PomeloMySql.Tests/Models/ForMapping
LinqToDB.EntityFrameworkCore.PostgreSQL.Tests/Models/ForMapping
LinqToDB.EntityFrameworkCore.SQLite.Tests/Models/ForMapping
LinqToDB.EntityFrameworkCore.SqlServer.Tests/Models/ForMapping Expand file tree Collapse file tree 8 files changed +91
-9
lines changed Original file line number Diff line number Diff line change @@ -112,16 +112,9 @@ public MappingAttribute[] GetAttributes(Type type)
112
112
{
113
113
foreach ( var e in _model . GetEntityTypes ( ) )
114
114
{
115
- if ( e . BaseType == et && e . GetDiscriminatorValue ( ) != null )
115
+ if ( GetBaseTypeRecursive ( e ) == et && e . GetDiscriminatorValue ( ) != null )
116
116
{
117
-
118
- result . Add (
119
- new InheritanceMappingAttribute ( )
120
- {
121
- Type = e . ClrType ,
122
- Code = e . GetDiscriminatorValue ( )
123
- }
124
- ) ;
117
+ result . AddRange ( GetMappingAttributesRecursive ( e ) ) ;
125
118
}
126
119
}
127
120
}
@@ -137,6 +130,31 @@ public MappingAttribute[] GetAttributes(Type type)
137
130
return result == null ? Array . Empty < MappingAttribute > ( ) : result . ToArray ( ) ;
138
131
}
139
132
133
+ static IEntityType GetBaseTypeRecursive ( IEntityType entityType )
134
+ {
135
+ if ( entityType . BaseType == null )
136
+ return entityType ;
137
+ return GetBaseTypeRecursive ( entityType . BaseType ) ;
138
+ }
139
+
140
+ static IEnumerable < InheritanceMappingAttribute > GetMappingAttributesRecursive ( IEntityType entityType )
141
+ {
142
+ var mappings = new List < InheritanceMappingAttribute > ( ) ;
143
+ return ProcessEntityType ( entityType ) ;
144
+
145
+ List < InheritanceMappingAttribute > ProcessEntityType ( IEntityType et )
146
+ {
147
+ mappings . Add ( new ( )
148
+ {
149
+ Type = et . ClrType , Code = entityType . GetDiscriminatorValue ( )
150
+ } ) ;
151
+
152
+ if ( et . BaseType == null )
153
+ return mappings ;
154
+ return ProcessEntityType ( et . BaseType ) ;
155
+ }
156
+ }
157
+
140
158
static bool CompareProperty ( MemberInfo ? property , MemberInfo memberInfo )
141
159
{
142
160
if ( property == memberInfo )
Original file line number Diff line number Diff line change @@ -161,5 +161,22 @@ public virtual void TestMappingSchemaCachedWithCustomSchema()
161
161
pk . IsIdentity . Should ( ) . BeFalse ( ) ;
162
162
Assert . AreEqual ( "Field" , pk . ColumnName ) ;
163
163
}
164
+
165
+ [ Test ]
166
+ public virtual async Task TestInheritance ( )
167
+ {
168
+ using var context = CreateContext ( ) ;
169
+ using var connection = context . CreateLinqToDBConnection ( ) ;
170
+
171
+ context . WithInheritance . AddRange ( new List < WithInheritanceA1 > ( ) { new ( ) { } , new ( ) { } } ) ;
172
+ context . WithInheritance . AddRange ( new List < WithInheritanceA2 > ( ) { new ( ) { } , new ( ) { } } ) ;
173
+ await context . SaveChangesAsync ( ) ;
174
+
175
+ var result = context . GetTable < WithInheritanceA > ( ) . ToList ( ) ;
176
+
177
+ result . OfType < WithInheritance > ( ) . Should ( ) . HaveCount ( 4 ) ;
178
+ result . OfType < WithInheritanceA1 > ( ) . Should ( ) . HaveCount ( 2 ) ;
179
+ result . OfType < WithInheritanceA2 > ( ) . Should ( ) . HaveCount ( 2 ) ;
180
+ }
164
181
}
165
182
}
Original file line number Diff line number Diff line change @@ -14,5 +14,10 @@ protected ForMappingContextBase(DbContextOptions options) : base(options)
14
14
public DbSet < StringTypes > StringTypes { get ; set ; } = null ! ;
15
15
16
16
public DbSet < WithDuplicateProperties > WithDuplicateProperties { get ; set ; } = null ! ;
17
+
18
+ public DbSet < WithInheritance > WithInheritance { get ; set ; } = null ! ;
19
+ public DbSet < WithInheritanceA > WithInheritanceA { get ; set ; } = null ! ;
20
+ public DbSet < WithInheritanceA1 > WithInheritanceA1 { get ; set ; } = null ! ;
21
+ public DbSet < WithInheritanceA2 > WithInheritanceA2 { get ; set ; } = null ! ;
17
22
}
18
23
}
Original file line number Diff line number Diff line change
1
+ namespace LinqToDB . EntityFrameworkCore . BaseTests . Models . ForMapping ;
2
+
3
+ public class WithInheritance
4
+ {
5
+ public int Id { get ; set ; }
6
+ public string Discriminator { get ; set ; } = null ! ;
7
+ }
8
+
9
+ public class WithInheritanceA : WithInheritance
10
+ {
11
+
12
+ }
13
+
14
+ public class WithInheritanceA1 : WithInheritanceA
15
+ {
16
+
17
+ }
18
+
19
+ public class WithInheritanceA2 : WithInheritanceA
20
+ {
21
+
22
+ }
Original file line number Diff line number Diff line change @@ -25,6 +25,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
25
25
{
26
26
b . HasKey ( e => e . Id ) ;
27
27
} ) ;
28
+
29
+ modelBuilder . Entity < WithInheritance > ( b =>
30
+ {
31
+ b . HasDiscriminator ( x => x . Discriminator ) ;
32
+ } ) ;
28
33
}
29
34
}
30
35
}
Original file line number Diff line number Diff line change @@ -23,6 +23,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
23
23
{
24
24
b . HasKey ( e => e . Id ) ;
25
25
} ) ;
26
+
27
+ modelBuilder . Entity < WithInheritance > ( b =>
28
+ {
29
+ b . HasDiscriminator ( x => x . Discriminator ) ;
30
+ } ) ;
26
31
}
27
32
}
28
33
}
Original file line number Diff line number Diff line change @@ -20,6 +20,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
20
20
{
21
21
b . HasKey ( e => e . Id ) ;
22
22
} ) ;
23
+
24
+ modelBuilder . Entity < WithInheritance > ( b =>
25
+ {
26
+ b . HasDiscriminator ( x => x . Discriminator ) ;
27
+ } ) ;
23
28
}
24
29
}
25
30
}
Original file line number Diff line number Diff line change @@ -30,6 +30,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
30
30
b . Property ( e => e . UnicodeString ) . HasMaxLength ( 50 ) . IsUnicode ( ) ;
31
31
}
32
32
) ;
33
+
34
+ modelBuilder . Entity < WithInheritance > ( b =>
35
+ {
36
+ b . HasDiscriminator ( x => x . Discriminator ) ;
37
+ } ) ;
33
38
}
34
39
}
35
40
}
You can’t perform that action at this time.
0 commit comments