Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,32 @@ public TableMetadata(IEntityType entityType, SqlDialectBuilder dialect)
Columns = GetColumns(entityType, dialect);
}

private static bool CanHandleProperty(IProperty property)
{
if (property.PropertyInfo == null || property.IsShadowProperty())
{
return false;
}

var getMethod = property.PropertyInfo.GetGetMethod();
if (getMethod == null || getMethod.GetParameters().Length > 0)
{
return false;
}

return true;
}

private static ColumnMetadata[] GetColumns(IEntityType entityType, SqlDialectBuilder dialect)
{
var properties = entityType.GetProperties()
.Where(p => !p.IsShadowProperty())
.Where(CanHandleProperty)
.Select(x => new ColumnMetadata(x, dialect));

var complexProperties = entityType.GetComplexProperties()
.SelectMany(cp => cp.ComplexType
.GetProperties()
.Where(p => !p.IsShadowProperty())
.Where(CanHandleProperty)
.Select(x => new ColumnMetadata(x, dialect, cp)));

return properties.Concat(complexProperties).ToArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

public class Course
{
public int Id { get; set; }
public string Title { get; set; } = null!;
public ICollection<Student> Students { get; set; } = new List<Student>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

public class Student
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public ICollection<Course> Courses { get; set; } = new List<Course>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class TestDbContext : TestDbContextBase
public DbSet<TestEntityWithGuidId> TestEntitiesWithGuidId { get; set; } = null!;
public DbSet<TestEntityWithConverters> TestEntitiesWithConverter { get; set; } = null!;
public DbSet<TestEntityWithComplexType> TestEntitiesWithComplexType { get; set; } = null!;
public DbSet<Student> Students { get; set; } = null!;
public DbSet<Course> Courses { get; set; } = null!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand All @@ -34,6 +36,21 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.ComplexProperty(e => e.OwnedComplexType)
.IsRequired();
});

// Many-to-many with shadow property
modelBuilder.Entity<Student>()
.HasMany(s => s.Courses)
.WithMany(c => c.Students)
.UsingEntity<Dictionary<string, object>>(
"StudentCourse",
j => j.HasOne<Course>().WithMany().HasForeignKey("CourseId"),
j => j.HasOne<Student>().WithMany().HasForeignKey("StudentId"),
j =>
{
j.Property<DateTime>("EnrolledAt");
j.HasKey("StudentId", "CourseId");
}
);
}
}

Expand Down Expand Up @@ -131,6 +148,3 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
});
}
}