Skip to content

Commit b23630d

Browse files
committed
Fix the System.InvalidOperationException: 'SqliteConnection does not support nested transactions issue
1 parent bcd0539 commit b23630d

File tree

7 files changed

+34
-26
lines changed

7 files changed

+34
-26
lines changed

NorthwindCRUD/Helpers/MappingProfiles.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ public MappingProfiles()
1818
CreateMap<TerritoryDto, TerritoryDb>().ReverseMap();
1919
CreateMap<CustomerDto, CustomerDb>().ReverseMap();
2020
CreateMap<OrderDto, OrderDb>().ReverseMap();
21-
CreateMap<CustomerWithOrdersDto, CustomerDb>().ReverseMap()
22-
.ForMember(dest => dest.Orders, opt => opt.MapFrom(src => src.Orders.ToArray()));
23-
CreateMap<OrderWithDetailsDto, OrderDb>().ReverseMap()
24-
.ForMember(dest => dest.OrderDetails, opt => opt.MapFrom(src => src.OrderDetails.ToArray()));
21+
CreateMap<CustomerWithOrdersDto, CustomerDb>().ReverseMap();
22+
CreateMap<OrderWithDetailsDto, OrderDb>().ReverseMap();
2523
CreateMap<OrderDetailDto, OrderDetailDb>().ReverseMap();
2624
CreateMap<AddressDto, AddressDb>().ReverseMap();
2725
CreateMap<LoginDto, UserDb>().ReverseMap();

NorthwindCRUD/Models/DbModels/ProductDb.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class ProductDb : IProduct
1010
[DatabaseGenerated(DatabaseGeneratedOption.None)]
1111
public int ProductId { get; set; }
1212

13+
public string Productname { get; set; }
14+
1315
public int? SupplierId { get; set; }
1416

1517
public SupplierDb? Supplier { get; set; }

NorthwindCRUD/Models/Dtos/ProductDto.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public class ProductDto : IProduct
77
{
88
public int ProductId { get; set; }
99

10+
public string ProductName { get; set; }
11+
1012
[Range(1, int.MaxValue, ErrorMessage = "SupplierId must be a valid supplier ID.")]
1113
public int? SupplierId { get; set; }
1214

NorthwindCRUD/Providers/DbContextConfigurationProvider.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ public void ConfigureOptions(DbContextOptionsBuilder options)
4242
var connectionString = this.GetSqlLiteConnectionString(tenantId);
4343
connection = new SqliteConnection(connectionString);
4444
memoryCache.Set(cacheKey, connection, GetCacheConnectionEntryOptions());
45-
}
4645

47-
// For SQLite in memory to be shared across multiple EF calls, we need to maintain a separate open connection.
48-
// see post https://stackoverflow.com/questions/56319638/entityframeworkcore-sqlite-in-memory-db-tables-are-not-created
49-
connection.Open();
46+
// For SQLite in memory to be shared across multiple EF calls, we need to maintain a separate open connection.
47+
// see post https://stackoverflow.com/questions/56319638/entityframeworkcore-sqlite-in-memory-db-tables-are-not-created
48+
connection.Open();
5049

51-
options.UseSqlite(connection).EnableSensitiveDataLogging();
50+
options.UseSqlite(connection).EnableSensitiveDataLogging();
5251

53-
SeedDb(options);
52+
SeedDb(options);
53+
}
54+
else
55+
{
56+
options.UseSqlite(connection).EnableSensitiveDataLogging();
57+
}
5458
}
5559
}
5660

NorthwindCRUD/Resources/products.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@
253253
},
254254
{
255255
"productId": 22,
256-
"productName": "Gustaf's Kn�ckebr�d",
256+
"productName": "Gustaf's Knäckebröd",
257257
"supplierId": 9,
258258
"categoryId": 5,
259259
"quantityPerUnit": "24 - 500 g pkgs.",
@@ -265,7 +265,7 @@
265265
},
266266
{
267267
"productId": 23,
268-
"productName": "Tunnbr�d",
268+
"productName": "Tunnbröd",
269269
"supplierId": 9,
270270
"categoryId": 5,
271271
"quantityPerUnit": "12 - 250 g pkgs.",
@@ -277,7 +277,7 @@
277277
},
278278
{
279279
"productId": 24,
280-
"productName": "Guaran� Fant�stica",
280+
"productName": "Guaraná Fantástica",
281281
"supplierId": 10,
282282
"categoryId": 1,
283283
"quantityPerUnit": "12 - 355 ml cans",
@@ -289,7 +289,7 @@
289289
},
290290
{
291291
"productId": 25,
292-
"productName": "NuNuCa Nu�-Nougat-Creme",
292+
"productName": "NuNuCa Nuß-Nougat-Creme",
293293
"supplierId": 11,
294294
"categoryId": 3,
295295
"quantityPerUnit": "20 - 450 g glasses",
@@ -301,7 +301,7 @@
301301
},
302302
{
303303
"productId": 26,
304-
"productName": "Gumb�r Gummib�rchen",
304+
"productName": "Gumbär Gummibärchen",
305305
"supplierId": 11,
306306
"categoryId": 3,
307307
"quantityPerUnit": "100 - 250 g bags",
@@ -325,7 +325,7 @@
325325
},
326326
{
327327
"productId": 28,
328-
"productName": "R�ssle Sauerkraut",
328+
"productName": "Rössle Sauerkraut",
329329
"supplierId": 12,
330330
"categoryId": 7,
331331
"quantityPerUnit": "25 - 825 g cans",
@@ -337,7 +337,7 @@
337337
},
338338
{
339339
"productId": 29,
340-
"productName": "Th�ringer Rostbratwurst",
340+
"productName": "Thüringer Rostbratwurst",
341341
"supplierId": 12,
342342
"categoryId": 6,
343343
"quantityPerUnit": "50 bags x 30 sausgs.",
@@ -445,7 +445,7 @@
445445
},
446446
{
447447
"productId": 38,
448-
"productName": "C�te de Blaye",
448+
"productName": "Côte de Blaye",
449449
"supplierId": 18,
450450
"categoryId": 1,
451451
"quantityPerUnit": "12 - 75 cl bottles",
@@ -637,7 +637,7 @@
637637
},
638638
{
639639
"productId": 54,
640-
"productName": "Tourti�re",
640+
"productName": "Tourtière",
641641
"supplierId": 25,
642642
"categoryId": 6,
643643
"quantityPerUnit": "16 pies",
@@ -649,7 +649,7 @@
649649
},
650650
{
651651
"productId": 55,
652-
"productName": "P�t� chinois",
652+
"productName": "Pâté chinois",
653653
"supplierId": 25,
654654
"categoryId": 6,
655655
"quantityPerUnit": "24 boxes x 2 pies",
@@ -721,7 +721,7 @@
721721
},
722722
{
723723
"productId": 61,
724-
"productName": "Sirop d'�rable",
724+
"productName": "Sirop d'érable",
725725
"supplierId": 29,
726726
"categoryId": 2,
727727
"quantityPerUnit": "24 - 500 ml bottles",
@@ -757,7 +757,7 @@
757757
},
758758
{
759759
"productId": 64,
760-
"productName": "Wimmers gute Semmelkn�del",
760+
"productName": "Wimmers gute Semmelknödel",
761761
"supplierId": 12,
762762
"categoryId": 5,
763763
"quantityPerUnit": "20 bags x 4 pieces",
@@ -865,7 +865,7 @@
865865
},
866866
{
867867
"productId": 73,
868-
"productName": "R�d Kaviar",
868+
"productName": "Röd Kaviar",
869869
"supplierId": 17,
870870
"categoryId": 8,
871871
"quantityPerUnit": "24 - 150 g jars",
@@ -889,7 +889,7 @@
889889
},
890890
{
891891
"productId": 75,
892-
"productName": "Rh�nbr�u Klosterbier",
892+
"productName": "Rhönbräu Klosterbier",
893893
"supplierId": 12,
894894
"categoryId": 1,
895895
"quantityPerUnit": "24 - 0.5 l bottles",
@@ -901,7 +901,7 @@
901901
},
902902
{
903903
"productId": 76,
904-
"productName": "Lakkalik��ri",
904+
"productName": "Lakkalikööri",
905905
"supplierId": 23,
906906
"categoryId": 1,
907907
"quantityPerUnit": "500 ml",
@@ -913,7 +913,7 @@
913913
},
914914
{
915915
"productId": 77,
916-
"productName": "Original Frankfurter gr�ne So�e",
916+
"productName": "Original Frankfurter grüne Soße",
917917
"supplierId": 12,
918918
"categoryId": 2,
919919
"quantityPerUnit": "12 boxes",

NorthwindCRUD/Services/CustomerService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public CustomerDb[] GetAllCustomersWithOrders()
3838
.Include(c => c.Address)
3939
.Include(c => c.Orders)
4040
.ThenInclude(o => o.OrderDetails)
41+
.AsNoTracking()
4142
.ToArray();
4243
}
4344

NorthwindCRUD/Services/OrderService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public OrderDb[] GetOrdersWithDetailsByCustomerId(string id)
5858
{
5959
return GetOrdersWithDetailsQuery()
6060
.Where(o => o.CustomerId == id)
61+
.AsNoTracking()
6162
.ToArray();
6263
}
6364

0 commit comments

Comments
 (0)