Skip to content

Commit 05bc259

Browse files
committed
.Net 10
1 parent 62fd166 commit 05bc259

7 files changed

Lines changed: 197 additions & 16 deletions

File tree

src/EntityFrameworkCore.SqlServer.SimpleBulks.Tests/Database/ComplexOwnedTypeOrder.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@ public class ComplexOwnedTypeOrder
66

77
public ComplexTypeAddress ComplexShippingAddress { get; set; }
88

9+
public OwnedTypeAddress OwnedShippingAddress { get; set; }
10+
}
11+
12+
public class JsonComplexOwnedTypeOrder
13+
{
14+
public int Id { get; set; }
15+
16+
public ComplexTypeAddress ComplexShippingAddress { get; set; }
17+
918
public OwnedTypeAddress OwnedShippingAddress { get; set; }
1019
}

src/EntityFrameworkCore.SqlServer.SimpleBulks.Tests/Database/ComplexTypeOrder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ public class ComplexTypeLocation
2323
public double Lat { get; set; }
2424

2525
public double Lng { get; set; }
26+
}
27+
28+
public class JsonComplexTypeOrder
29+
{
30+
public int Id { get; set; }
31+
32+
public ComplexTypeAddress ShippingAddress { get; set; }
2633
}

src/EntityFrameworkCore.SqlServer.SimpleBulks.Tests/Database/JsonComplexTypeOrder.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/EntityFrameworkCore.SqlServer.SimpleBulks.Tests/Database/JsonOwnedTypeOrder.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/EntityFrameworkCore.SqlServer.SimpleBulks.Tests/Database/OwnedTypeOrder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ public class OwnedTypeLocation
2323
public double Lat { get; set; }
2424

2525
public double Lng { get; set; }
26+
}
27+
28+
public class JsonOwnedTypeOrder
29+
{
30+
public int Id { get; set; }
31+
32+
public OwnedTypeAddress ShippingAddress { get; set; }
2633
}

src/EntityFrameworkCore.SqlServer.SimpleBulks.Tests/Database/TestDbContext.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class TestDbContext : DbContext
2525

2626
public DbSet<JsonOwnedTypeOrder> JsonOwnedTypeOrders { get; set; }
2727

28+
public DbSet<JsonComplexOwnedTypeOrder> JsonComplexOwnedTypeOrders { get; set; }
29+
2830
public DbSet<Blog> Blogs { get; set; }
2931

3032
public DbSet<RssBlog> RssBlogs { get; set; }
@@ -83,6 +85,26 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
8385
});
8486
});
8587

88+
modelBuilder.Entity<JsonComplexOwnedTypeOrder>().ComplexProperty(x => x.ComplexShippingAddress, x =>
89+
{
90+
x.ToJson();
91+
//x.ToJson("xxx").HasColumnType("json");
92+
x.ComplexProperty(y => y.Location, y =>
93+
{
94+
y.HasJsonPropertyName("xxx");
95+
});
96+
});
97+
98+
modelBuilder.Entity<JsonComplexOwnedTypeOrder>().OwnsOne(x => x.OwnedShippingAddress, x =>
99+
{
100+
x.ToJson();
101+
//x.ToJson("xxx").HasColumnType("json");
102+
x.OwnsOne(y => y.Location, y =>
103+
{
104+
y.HasJsonPropertyName("xxx");
105+
});
106+
});
107+
86108
base.OnModelCreating(modelBuilder);
87109
}
88110
}

src/EntityFrameworkCore.SqlServer.SimpleBulks.Tests/DbContextExtensions/GetPropertiesTests.cs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,156 @@ public void GetProperties_JsonOwnedType_ReturnsCorrectColumnInformation()
472472
Assert.Contains("Lat", json);
473473
Assert.Contains("Lng", json);
474474
}
475+
476+
[Fact]
477+
public void GetProperties_JsonComplexOwnedType_ReturnsCorrectColumnInformation()
478+
{
479+
// Arrange
480+
var dbContext = new TestDbContext("", "");
481+
482+
// Act
483+
var properties = dbContext.GetProperties(typeof(JsonComplexOwnedTypeOrder));
484+
485+
// Assert
486+
Assert.Equal(3, properties.Count);
487+
488+
var property = properties.First(p => p.PropertyName == "Id");
489+
Assert.Equal(typeof(int), property.PropertyType);
490+
Assert.Equal("Id", property.ColumnName);
491+
Assert.Equal("int", property.ColumnType);
492+
Assert.Equal(ValueGenerated.OnAdd, property.ValueGenerated);
493+
Assert.Null(property.DefaultValueSql);
494+
Assert.True(property.IsPrimaryKey);
495+
Assert.False(property.IsRowVersion);
496+
497+
property = properties.First(p => p.PropertyName == "ComplexShippingAddress");
498+
Assert.Equal(typeof(ComplexTypeAddress), property.PropertyType);
499+
Assert.Equal("ComplexShippingAddress", property.ColumnName);
500+
Assert.Equal("nvarchar(max)", property.ColumnType);
501+
Assert.Equal(ValueGenerated.Never, property.ValueGenerated);
502+
Assert.Null(property.DefaultValueSql);
503+
Assert.False(property.IsPrimaryKey);
504+
Assert.False(property.IsRowVersion);
505+
Assert.True(property.IsJson);
506+
Assert.NotNull(property.JsonPropertyWriters);
507+
508+
// Verify that the JsonPropertyWriters have correct structure
509+
var writers = property.JsonPropertyWriters;
510+
var flattenedWriters = property.GetFlattenedJsonPropertyWriters();
511+
Assert.Equal(2, writers.Count); // Street and Location
512+
513+
var streetWriter = writers.FirstOrDefault(w => w.ClrPropertyName == "Street");
514+
Assert.NotNull(streetWriter);
515+
Assert.Equal("Street", streetWriter.JsonPropertyName);
516+
Assert.Equal("Street", streetWriter.FullJsonPath);
517+
Assert.Equal("Street", streetWriter.FullClrPropertyName);
518+
Assert.Equal(typeof(string), streetWriter.PropertyType);
519+
Assert.False(streetWriter.IsNestedComplexType);
520+
521+
var locationWriter = writers.FirstOrDefault(w => w.ClrPropertyName == "Location");
522+
Assert.NotNull(locationWriter);
523+
Assert.Equal("xxx", locationWriter.JsonPropertyName); // Mapped to "xxx" in TestDbContext
524+
Assert.Equal("xxx", locationWriter.FullJsonPath);
525+
Assert.Equal("Location", locationWriter.FullClrPropertyName);
526+
Assert.Equal(typeof(ComplexTypeLocation), locationWriter.PropertyType);
527+
Assert.True(locationWriter.IsNestedComplexType);
528+
Assert.NotNull(locationWriter.NestedProperties);
529+
Assert.Equal(2, locationWriter.NestedProperties.Count); // Lat and Lng
530+
531+
var latWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lat");
532+
Assert.NotNull(latWriter);
533+
Assert.Equal("xxx.Lat", latWriter.FullJsonPath);
534+
Assert.Equal("Location.Lat", latWriter.FullClrPropertyName);
535+
Assert.Equal(typeof(double), latWriter.PropertyType);
536+
537+
var lngWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lng");
538+
Assert.NotNull(lngWriter);
539+
Assert.Equal("xxx.Lng", lngWriter.FullJsonPath);
540+
Assert.Equal("Location.Lng", lngWriter.FullClrPropertyName);
541+
Assert.Equal(typeof(double), lngWriter.PropertyType);
542+
543+
// Verify serialization using JsonPropertyWriters
544+
var testComplexAddress = new ComplexTypeAddress
545+
{
546+
Street = "123 Main St",
547+
Location = new ComplexTypeLocation
548+
{
549+
Lat = 40.7128,
550+
Lng = -74.0060
551+
}
552+
};
553+
554+
var json = SerializeWithJsonPropertyWriters(testComplexAddress, writers);
555+
Assert.NotNull(json);
556+
Assert.Contains("Street", json);
557+
Assert.Contains("123 Main St", json);
558+
Assert.Contains("xxx", json); // Location is mapped to "xxx"
559+
Assert.Contains("Lat", json);
560+
Assert.Contains("Lng", json);
561+
562+
property = properties.First(p => p.PropertyName == "OwnedShippingAddress");
563+
Assert.Equal(typeof(OwnedTypeAddress), property.PropertyType);
564+
Assert.Equal("OwnedShippingAddress", property.ColumnName);
565+
Assert.Equal("nvarchar(max)", property.ColumnType);
566+
Assert.Equal(ValueGenerated.Never, property.ValueGenerated);
567+
Assert.Null(property.DefaultValueSql);
568+
Assert.False(property.IsPrimaryKey);
569+
Assert.False(property.IsRowVersion);
570+
Assert.True(property.IsJson);
571+
Assert.NotNull(property.JsonPropertyWriters);
572+
573+
// Verify that the JsonPropertyWriters have correct structure
574+
writers = property.JsonPropertyWriters;
575+
flattenedWriters = property.GetFlattenedJsonPropertyWriters();
576+
Assert.Equal(3, writers.Count); // Street and Location
577+
578+
streetWriter = writers.FirstOrDefault(w => w.ClrPropertyName == "Street");
579+
Assert.NotNull(streetWriter);
580+
Assert.Equal("Street", streetWriter.JsonPropertyName);
581+
Assert.Equal("Street", streetWriter.FullJsonPath);
582+
Assert.Equal("Street", streetWriter.FullClrPropertyName);
583+
Assert.Equal(typeof(string), streetWriter.PropertyType);
584+
Assert.False(streetWriter.IsNestedComplexType);
585+
586+
locationWriter = writers.FirstOrDefault(w => w.ClrPropertyName == "Location");
587+
Assert.NotNull(locationWriter);
588+
Assert.Equal("xxx", locationWriter.JsonPropertyName); // Mapped to "xxx" in TestDbContext
589+
Assert.Equal("xxx", locationWriter.FullJsonPath);
590+
Assert.Equal("Location", locationWriter.FullClrPropertyName);
591+
Assert.Equal(typeof(OwnedTypeLocation), locationWriter.PropertyType);
592+
Assert.True(locationWriter.IsNestedComplexType);
593+
Assert.NotNull(locationWriter.NestedProperties);
594+
Assert.Equal(3, locationWriter.NestedProperties.Count); // Lat and Lng
595+
596+
latWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lat");
597+
Assert.NotNull(latWriter);
598+
Assert.Equal("xxx.Lat", latWriter.FullJsonPath);
599+
Assert.Equal("Location.Lat", latWriter.FullClrPropertyName);
600+
Assert.Equal(typeof(double), latWriter.PropertyType);
601+
602+
lngWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lng");
603+
Assert.NotNull(lngWriter);
604+
Assert.Equal("xxx.Lng", lngWriter.FullJsonPath);
605+
Assert.Equal("Location.Lng", lngWriter.FullClrPropertyName);
606+
Assert.Equal(typeof(double), lngWriter.PropertyType);
607+
608+
// Verify serialization using JsonPropertyWriters
609+
var testOwnedAddress = new OwnedTypeAddress
610+
{
611+
Street = "123 Main St",
612+
Location = new OwnedTypeLocation
613+
{
614+
Lat = 40.7128,
615+
Lng = -74.0060
616+
}
617+
};
618+
619+
json = SerializeWithJsonPropertyWriters(testOwnedAddress, writers);
620+
Assert.NotNull(json);
621+
Assert.Contains("Street", json);
622+
Assert.Contains("123 Main St", json);
623+
Assert.Contains("xxx", json); // Location is mapped to "xxx"
624+
Assert.Contains("Lat", json);
625+
Assert.Contains("Lng", json);
626+
}
475627
}

0 commit comments

Comments
 (0)