Skip to content

Commit 21e7863

Browse files
authored
Fixes #63 : cannot handle shadow properties, or properties without getter, or properties with a getter with arguments (#73)
1 parent b2751f7 commit 21e7863

4 files changed

Lines changed: 51 additions & 5 deletions

File tree

src/PhenX.EntityFrameworkCore.BulkInsert/Metadata/TableMetadata.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,32 @@ public TableMetadata(IEntityType entityType, SqlDialectBuilder dialect)
2626
Columns = GetColumns(entityType, dialect);
2727
}
2828

29+
private static bool CanHandleProperty(IProperty property)
30+
{
31+
if (property.PropertyInfo == null || property.IsShadowProperty())
32+
{
33+
return false;
34+
}
35+
36+
var getMethod = property.PropertyInfo.GetGetMethod();
37+
if (getMethod == null || getMethod.GetParameters().Length > 0)
38+
{
39+
return false;
40+
}
41+
42+
return true;
43+
}
44+
2945
private static ColumnMetadata[] GetColumns(IEntityType entityType, SqlDialectBuilder dialect)
3046
{
3147
var properties = entityType.GetProperties()
32-
.Where(p => !p.IsShadowProperty())
48+
.Where(CanHandleProperty)
3349
.Select(x => new ColumnMetadata(x, dialect));
3450

3551
var complexProperties = entityType.GetComplexProperties()
3652
.SelectMany(cp => cp.ComplexType
3753
.GetProperties()
38-
.Where(p => !p.IsShadowProperty())
54+
.Where(CanHandleProperty)
3955
.Select(x => new ColumnMetadata(x, dialect, cp)));
4056

4157
return properties.Concat(complexProperties).ToArray();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
2+
3+
public class Course
4+
{
5+
public int Id { get; set; }
6+
public string Title { get; set; } = null!;
7+
public ICollection<Student> Students { get; set; } = new List<Student>();
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;
2+
3+
public class Student
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; } = null!;
7+
public ICollection<Course> Courses { get; set; } = new List<Course>();
8+
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestDbContext.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class TestDbContext : TestDbContextBase
1111
public DbSet<TestEntityWithGuidId> TestEntitiesWithGuidId { get; set; } = null!;
1212
public DbSet<TestEntityWithConverters> TestEntitiesWithConverter { get; set; } = null!;
1313
public DbSet<TestEntityWithComplexType> TestEntitiesWithComplexType { get; set; } = null!;
14+
public DbSet<Student> Students { get; set; } = null!;
15+
public DbSet<Course> Courses { get; set; } = null!;
1416

1517
protected override void OnModelCreating(ModelBuilder modelBuilder)
1618
{
@@ -34,6 +36,21 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3436
.ComplexProperty(e => e.OwnedComplexType)
3537
.IsRequired();
3638
});
39+
40+
// Many-to-many with shadow property
41+
modelBuilder.Entity<Student>()
42+
.HasMany(s => s.Courses)
43+
.WithMany(c => c.Students)
44+
.UsingEntity<Dictionary<string, object>>(
45+
"StudentCourse",
46+
j => j.HasOne<Course>().WithMany().HasForeignKey("CourseId"),
47+
j => j.HasOne<Student>().WithMany().HasForeignKey("StudentId"),
48+
j =>
49+
{
50+
j.Property<DateTime>("EnrolledAt");
51+
j.HasKey("StudentId", "CourseId");
52+
}
53+
);
3754
}
3855
}
3956

@@ -131,6 +148,3 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
131148
});
132149
}
133150
}
134-
135-
136-

0 commit comments

Comments
 (0)