@@ -478,10 +478,13 @@ public async Task Can_read_with_find_with_resource_id_async()
478
478
PartitionKey = pk1
479
479
} ;
480
480
481
- await using ( var context = new PartitionKeyContext_WithResourceId ( options ) )
481
+ await using ( var context = new PartitionKeyContextWithResourceId ( options ) )
482
482
{
483
483
await context . Database . EnsureCreatedAsync ( ) ;
484
484
485
+ Assert . Null ( context . Model . FindEntityType ( typeof ( Customer_WithResourceId ) )
486
+ . FindProperty ( StoreKeyConvention . DefaultIdPropertyName ) ) ;
487
+
485
488
context . Add ( customer ) ;
486
489
context . Add (
487
490
new Customer_WithResourceId
@@ -494,7 +497,7 @@ public async Task Can_read_with_find_with_resource_id_async()
494
497
await context . SaveChangesAsync ( ) ;
495
498
}
496
499
497
- await using ( var context = new PartitionKeyContext_WithResourceId ( options ) )
500
+ await using ( var context = new PartitionKeyContextWithResourceId ( options ) )
498
501
{
499
502
var customerFromStore = await context . Set < Customer_WithResourceId > ( )
500
503
. FindAsync ( pk1 , "42" ) ;
@@ -509,7 +512,7 @@ public async Task Can_read_with_find_with_resource_id_async()
509
512
await context . SaveChangesAsync ( ) ;
510
513
}
511
514
512
- await using ( var context = new PartitionKeyContext_WithResourceId ( options ) )
515
+ await using ( var context = new PartitionKeyContextWithResourceId ( options ) )
513
516
{
514
517
var customerFromStore = await context . Set < Customer_WithResourceId > ( )
515
518
. WithPartitionKey ( partitionKey : pk1 . ToString ( ) )
@@ -535,7 +538,7 @@ public void Can_read_with_find_with_resource_id()
535
538
PartitionKey = pk1
536
539
} ;
537
540
538
- using ( var context = new PartitionKeyContext_WithResourceId ( options ) )
541
+ using ( var context = new PartitionKeyContextWithResourceId ( options ) )
539
542
{
540
543
context . Database . EnsureCreated ( ) ;
541
544
@@ -551,7 +554,7 @@ public void Can_read_with_find_with_resource_id()
551
554
context . SaveChanges ( ) ;
552
555
}
553
556
554
- using ( var context = new PartitionKeyContext_WithResourceId ( options ) )
557
+ using ( var context = new PartitionKeyContextWithResourceId ( options ) )
555
558
{
556
559
var customerFromStore = context . Set < Customer_WithResourceId > ( )
557
560
. Find ( pk1 , "42" ) ;
@@ -566,7 +569,7 @@ public void Can_read_with_find_with_resource_id()
566
569
context . SaveChanges ( ) ;
567
570
}
568
571
569
- using ( var context = new PartitionKeyContext_WithResourceId ( options ) )
572
+ using ( var context = new PartitionKeyContextWithResourceId ( options ) )
570
573
{
571
574
var customerFromStore = context . Set < Customer_WithResourceId > ( )
572
575
. WithPartitionKey ( partitionKey : pk1 . ToString ( ) )
@@ -582,7 +585,7 @@ public void Can_read_with_find_with_resource_id()
582
585
public void Find_with_empty_resource_id_throws ( )
583
586
{
584
587
var options = Fixture . CreateOptions ( ) ;
585
- using ( var context = new PartitionKeyContext_WithResourceId ( options ) )
588
+ using ( var context = new PartitionKeyContextWithResourceId ( options ) )
586
589
{
587
590
context . Database . EnsureCreated ( ) ;
588
591
@@ -802,7 +805,7 @@ public async Task Can_read_with_find_without_partition_key()
802
805
Name = "Theon"
803
806
} ;
804
807
805
- await using ( var context = new PartitionKeyContext_EntityWithNoPartitionKey ( options ) )
808
+ await using ( var context = new PartitionKeyContextEntityWithNoPartitionKey ( options ) )
806
809
{
807
810
await context . Database . EnsureCreatedAsync ( ) ;
808
811
@@ -811,7 +814,7 @@ public async Task Can_read_with_find_without_partition_key()
811
814
await context . SaveChangesAsync ( ) ;
812
815
}
813
816
814
- await using ( var context = new PartitionKeyContext_EntityWithNoPartitionKey ( options ) )
817
+ await using ( var context = new PartitionKeyContextEntityWithNoPartitionKey ( options ) )
815
818
{
816
819
var customerFromStore = context . Set < Customer_NoPartitionKey > ( ) . Find ( 42 ) ;
817
820
@@ -821,6 +824,71 @@ public async Task Can_read_with_find_without_partition_key()
821
824
}
822
825
}
823
826
827
+ [ ConditionalFact ]
828
+ public async Task Can_read_with_find_with_PK_partition_key ( )
829
+ {
830
+ var options = Fixture . CreateOptions ( ) ;
831
+
832
+ var customer = new Customer
833
+ {
834
+ Id = 42 ,
835
+ Name = "Theon"
836
+ } ;
837
+
838
+ await using ( var context = new PartitionKeyContextPrimaryKey ( options ) )
839
+ {
840
+ await context . Database . EnsureCreatedAsync ( ) ;
841
+
842
+ context . Add ( customer ) ;
843
+
844
+ await context . SaveChangesAsync ( ) ;
845
+ }
846
+
847
+ await using ( var context = new PartitionKeyContextPrimaryKey ( options ) )
848
+ {
849
+ var customerFromStore = context . Set < Customer > ( ) . Find ( 42 ) ;
850
+
851
+ Assert . Equal ( 42 , customerFromStore . Id ) ;
852
+ Assert . Equal ( "Theon" , customerFromStore . Name ) ;
853
+ AssertSql ( context , @"ReadItem(42, 42)" ) ;
854
+ }
855
+ }
856
+
857
+ [ ConditionalFact ]
858
+ public async Task Can_read_with_find_with_PK_resource_id ( )
859
+ {
860
+ var options = Fixture . CreateOptions ( ) ;
861
+
862
+ var customer = new Customer_WithResourceId
863
+ {
864
+ id = "42" ,
865
+ Name = "Theon"
866
+ } ;
867
+
868
+ await using ( var context = new PartitionKeyContextWithPrimaryKeyResourceId ( options ) )
869
+ {
870
+ await context . Database . EnsureCreatedAsync ( ) ;
871
+
872
+ context . Add ( customer ) ;
873
+
874
+ await context . SaveChangesAsync ( ) ;
875
+ }
876
+
877
+ await using ( var context = new PartitionKeyContextWithPrimaryKeyResourceId ( options ) )
878
+ {
879
+ var customerFromStore = context . Set < Customer_WithResourceId > ( ) . Find ( "42" ) ;
880
+
881
+ Assert . Equal ( "42" , customerFromStore . id ) ;
882
+ Assert . Equal ( "Theon" , customerFromStore . Name ) ;
883
+ AssertSql ( context , @"@__p_0='42'
884
+
885
+ SELECT c
886
+ FROM root c
887
+ WHERE ((c[""Discriminator""] = ""Customer_WithResourceId"") AND (c[""id""] = @__p_0))
888
+ OFFSET 0 LIMIT 1" ) ;
889
+ }
890
+ }
891
+
824
892
private class PartitionKeyContext : DbContext
825
893
{
826
894
public PartitionKeyContext ( DbContextOptions dbContextOptions )
@@ -840,9 +908,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
840
908
}
841
909
}
842
910
843
- private class PartitionKeyContext_EntityWithNoPartitionKey : DbContext
911
+ private class PartitionKeyContextEntityWithNoPartitionKey : DbContext
844
912
{
845
- public PartitionKeyContext_EntityWithNoPartitionKey ( DbContextOptions dbContextOptions )
913
+ public PartitionKeyContextEntityWithNoPartitionKey ( DbContextOptions dbContextOptions )
846
914
: base ( dbContextOptions )
847
915
{
848
916
}
@@ -870,7 +938,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
870
938
cb . Property ( StoreKeyConvention . DefaultIdPropertyName )
871
939
. HasValueGenerator ( ( p , e ) => valueGeneratorFactory . Create ( p ) ) ;
872
940
873
- cb . Property ( c => c . Id ) ;
874
941
cb . Property ( c => c . PartitionKey ) . HasConversion < string > ( ) ;
875
942
876
943
cb . HasPartitionKey ( c => c . PartitionKey ) ;
@@ -895,7 +962,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
895
962
896
963
cb . Property ( StoreKeyConvention . DefaultIdPropertyName ) . HasValueGenerator ( ( Type ) null ) ;
897
964
898
- cb . Property ( c => c . Id ) ;
899
965
cb . Property ( c => c . PartitionKey ) . HasConversion < string > ( ) ;
900
966
901
967
cb . HasPartitionKey ( c => c . PartitionKey ) ;
@@ -911,22 +977,61 @@ public PartitionKeyContextNonPrimaryKey(DbContextOptions dbContextOptions)
911
977
{
912
978
}
913
979
980
+ protected override void OnModelCreating ( ModelBuilder modelBuilder )
981
+ {
982
+ modelBuilder . Entity < Customer > (
983
+ cb =>
984
+ {
985
+ cb . HasKey ( c => new { c . Id } ) ;
986
+ } ) ;
987
+ }
988
+ }
989
+
990
+ private class PartitionKeyContextPrimaryKey : DbContext
991
+ {
992
+ public PartitionKeyContextPrimaryKey ( DbContextOptions dbContextOptions )
993
+ : base ( dbContextOptions )
994
+ {
995
+ }
996
+
914
997
protected override void OnModelCreating ( ModelBuilder modelBuilder )
915
998
{
916
999
modelBuilder . Entity < Customer > (
917
1000
cb =>
918
1001
{
919
1002
var valueGeneratorFactory = new CustomPartitionKeyIdValueGeneratorFactory ( ) ;
920
1003
921
- cb . Property ( c => c . Id ) ;
1004
+ cb . HasNoDiscriminator ( ) ;
1005
+ cb . Property ( c => c . Id ) . HasConversion < string > ( ) ;
1006
+ cb . HasPartitionKey ( c => c . Id ) ;
922
1007
cb . HasKey ( c => new { c . Id } ) ;
923
1008
} ) ;
924
1009
}
925
1010
}
926
1011
927
- private class PartitionKeyContext_WithResourceId : DbContext
1012
+ private class PartitionKeyContextWithPrimaryKeyResourceId : DbContext
1013
+ {
1014
+ public PartitionKeyContextWithPrimaryKeyResourceId ( DbContextOptions dbContextOptions )
1015
+ : base ( dbContextOptions )
1016
+ {
1017
+ }
1018
+
1019
+ protected override void OnModelCreating ( ModelBuilder modelBuilder )
1020
+ {
1021
+ modelBuilder . Entity < Customer_WithResourceId > (
1022
+ cb =>
1023
+ {
1024
+ cb . HasPartitionKey ( c => c . PartitionKey ) ;
1025
+ cb . Property ( c => c . PartitionKey ) . HasConversion < string > ( ) ;
1026
+ cb . Property ( c => c . id ) . HasConversion < string > ( ) ;
1027
+ cb . HasKey ( c => new { c . id } ) ;
1028
+ } ) ;
1029
+ }
1030
+ }
1031
+
1032
+ private class PartitionKeyContextWithResourceId : DbContext
928
1033
{
929
- public PartitionKeyContext_WithResourceId ( DbContextOptions dbContextOptions )
1034
+ public PartitionKeyContextWithResourceId ( DbContextOptions dbContextOptions )
930
1035
: base ( dbContextOptions )
931
1036
{
932
1037
}
0 commit comments