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 @@ -109,16 +109,9 @@ public MappingAttribute[] GetAttributes(Type type)
109109 {
110110 foreach ( var e in _model . GetEntityTypes ( ) )
111111 {
112- if ( e . BaseType == et && e . GetDiscriminatorValue ( ) != null )
112+ if ( GetBaseTypeRecursive ( e ) == et && e . GetDiscriminatorValue ( ) != null )
113113 {
114-
115- result . Add (
116- new InheritanceMappingAttribute ( )
117- {
118- Type = e . ClrType ,
119- Code = e . GetDiscriminatorValue ( )
120- }
121- ) ;
114+ result . AddRange ( GetMappingAttributesRecursive ( e ) ) ;
122115 }
123116 }
124117 }
@@ -134,6 +127,31 @@ public MappingAttribute[] GetAttributes(Type type)
134127 return result == null ? Array . Empty < MappingAttribute > ( ) : result . ToArray ( ) ;
135128 }
136129
130+ static IEntityType GetBaseTypeRecursive ( IEntityType entityType )
131+ {
132+ if ( entityType . BaseType == null )
133+ return entityType ;
134+ return GetBaseTypeRecursive ( entityType . BaseType ) ;
135+ }
136+
137+ static IEnumerable < InheritanceMappingAttribute > GetMappingAttributesRecursive ( IEntityType entityType )
138+ {
139+ var mappings = new List < InheritanceMappingAttribute > ( ) ;
140+ return ProcessEntityType ( entityType ) ;
141+
142+ List < InheritanceMappingAttribute > ProcessEntityType ( IEntityType et )
143+ {
144+ mappings . Add ( new ( )
145+ {
146+ Type = et . ClrType , Code = entityType . GetDiscriminatorValue ( )
147+ } ) ;
148+
149+ if ( et . BaseType == null )
150+ return mappings ;
151+ return ProcessEntityType ( et . BaseType ) ;
152+ }
153+ }
154+
137155 static bool CompareProperty ( MemberInfo ? property , MemberInfo memberInfo )
138156 {
139157 if ( property == memberInfo )
Original file line number Diff line number Diff line change @@ -161,5 +161,22 @@ public virtual void TestMappingSchemaCachedWithCustomSchema()
161161 pk . IsIdentity . Should ( ) . BeFalse ( ) ;
162162 Assert . AreEqual ( "Field" , pk . ColumnName ) ;
163163 }
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+ }
164181 }
165182}
Original file line number Diff line number Diff line change @@ -14,5 +14,10 @@ protected ForMappingContextBase(DbContextOptions options) : base(options)
1414 public DbSet < StringTypes > StringTypes { get ; set ; } = null ! ;
1515
1616 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 ! ;
1722 }
1823}
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)
2525 {
2626 b . HasKey ( e => e . Id ) ;
2727 } ) ;
28+
29+ modelBuilder . Entity < WithInheritance > ( b =>
30+ {
31+ b . HasDiscriminator ( x => x . Discriminator ) ;
32+ } ) ;
2833 }
2934 }
3035}
Original file line number Diff line number Diff line change @@ -25,6 +25,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2525 {
2626 b . HasKey ( e => e . Id ) ;
2727 } ) ;
28+
29+ modelBuilder . Entity < WithInheritance > ( b =>
30+ {
31+ b . HasDiscriminator ( x => x . Discriminator ) ;
32+ } ) ;
2833 }
2934 }
3035}
Original file line number Diff line number Diff line change @@ -22,6 +22,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2222 {
2323 b . HasKey ( e => e . Id ) ;
2424 } ) ;
25+
26+ modelBuilder . Entity < WithInheritance > ( b =>
27+ {
28+ b . HasDiscriminator ( x => x . Discriminator ) ;
29+ } ) ;
2530 }
2631 }
2732}
Original file line number Diff line number Diff line change @@ -32,6 +32,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3232 b . Property ( e => e . UnicodeString ) . HasMaxLength ( 50 ) . IsUnicode ( ) ;
3333 }
3434 ) ;
35+
36+ modelBuilder . Entity < WithInheritance > ( b =>
37+ {
38+ b . HasDiscriminator ( x => x . Discriminator ) ;
39+ } ) ;
3540 }
3641 }
3742}
You can’t perform that action at this time.
0 commit comments