-
Notifications
You must be signed in to change notification settings - Fork 1
/
VsNorthwind.cs
2241 lines (1906 loc) · 111 KB
/
VsNorthwind.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ------------------------------------------------------------------------------------------------
// This code was generated by EntityFramework Reverse POCO Generator (http://www.reversepoco.co.uk/).
// Created by Simon Hughes (https://about.me/simon.hughes).
//
// Registered to: Simon Hughes
// Company : Reverse POCO
// Licence Type : Commercial
// Licences : 1
// Valid until : 03 NOV 2020
//
// Do not make changes directly to this file - edit the template instead.
//
// The following connection settings were used to generate this file:
// Connection String Name: "MyDbContext"
// Connection String: "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
// ------------------------------------------------------------------------------------------------
// Database Edition : Developer Edition (64-bit)
// Database Engine Edition: Enterprise
// Database Version : 14.0.2027.2
// <auto-generated>
// ReSharper disable CheckNamespace
// ReSharper disable ConvertPropertyToExpressionBody
// ReSharper disable DoNotCallOverridableMethodsInConstructor
// ReSharper disable EmptyNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable NotAccessedVariable
// ReSharper disable PartialMethodWithSinglePart
// ReSharper disable PartialTypeWithSinglePart
// ReSharper disable RedundantCast
// ReSharper disable RedundantNameQualifier
// ReSharper disable RedundantOverridenMember
// ReSharper disable UseNameofExpression
// ReSharper disable UsePatternMatching
#pragma warning disable 1591 // Ignore "Missing XML Comment" warning
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Query.Internal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace EfrpgVisualStudio
{
#region Database context interface
public interface IMyDbContext : IDisposable
{
DbSet<AlphabeticalListOfProduct> AlphabeticalListOfProducts { get; set; } // Alphabetical list of products
DbSet<Category> Categories { get; set; } // Categories
DbSet<CategorySalesFor1997> CategorySalesFor1997 { get; set; } // Category Sales for 1997
DbSet<CurrentProductList> CurrentProductLists { get; set; } // Current Product List
DbSet<Customer> Customers { get; set; } // Customers
DbSet<CustomerAndSuppliersByCity> CustomerAndSuppliersByCities { get; set; } // Customer and Suppliers by City
DbSet<CustomerCustomerDemo> CustomerCustomerDemoes { get; set; } // CustomerCustomerDemo
DbSet<CustomerDemographic> CustomerDemographics { get; set; } // CustomerDemographics
DbSet<Employee> Employees { get; set; } // Employees
DbSet<EmployeeTerritory> EmployeeTerritories { get; set; } // EmployeeTerritories
DbSet<Invoice> Invoices { get; set; } // Invoices
DbSet<Order> Orders { get; set; } // Orders
DbSet<OrderDetail> OrderDetails { get; set; } // Order Details
DbSet<OrderDetailsExtended> OrderDetailsExtendeds { get; set; } // Order Details Extended
DbSet<OrdersQry> OrdersQries { get; set; } // Orders Qry
DbSet<OrderSubtotal> OrderSubtotals { get; set; } // Order Subtotals
DbSet<Product> Products { get; set; } // Products
DbSet<ProductsAboveAveragePrice> ProductsAboveAveragePrices { get; set; } // Products Above Average Price
DbSet<ProductSalesFor1997> ProductSalesFor1997 { get; set; } // Product Sales for 1997
DbSet<ProductsByCategory> ProductsByCategories { get; set; } // Products by Category
DbSet<QuarterlyOrder> QuarterlyOrders { get; set; } // Quarterly Orders
DbSet<Region> Regions { get; set; } // Region
DbSet<SalesByCategory> SalesByCategories { get; set; } // Sales by Category
DbSet<SalesTotalsByAmount> SalesTotalsByAmounts { get; set; } // Sales Totals by Amount
DbSet<Shipper> Shippers { get; set; } // Shippers
DbSet<SummaryOfSalesByQuarter> SummaryOfSalesByQuarters { get; set; } // Summary of Sales by Quarter
DbSet<SummaryOfSalesByYear> SummaryOfSalesByYears { get; set; } // Summary of Sales by Year
DbSet<Supplier> Suppliers { get; set; } // Suppliers
DbSet<Territory> Territories { get; set; } // Territories
int SaveChanges();
int SaveChanges(bool acceptAllChangesOnSuccess);
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken));
Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken));
DatabaseFacade Database { get; }
DbSet<TEntity> Set<TEntity>() where TEntity : class;
string ToString();
// Stored Procedures
List<CustOrderHistReturnModel> CustOrderHist(string customerId);
List<CustOrderHistReturnModel> CustOrderHist(string customerId, out int procResult);
Task<List<CustOrderHistReturnModel>> CustOrderHistAsync(string customerId);
List<CustOrdersDetailReturnModel> CustOrdersDetail(int? orderId);
List<CustOrdersDetailReturnModel> CustOrdersDetail(int? orderId, out int procResult);
Task<List<CustOrdersDetailReturnModel>> CustOrdersDetailAsync(int? orderId);
List<CustOrdersOrdersReturnModel> CustOrdersOrders(string customerId);
List<CustOrdersOrdersReturnModel> CustOrdersOrders(string customerId, out int procResult);
Task<List<CustOrdersOrdersReturnModel>> CustOrdersOrdersAsync(string customerId);
List<EmployeeSalesByCountryReturnModel> EmployeeSalesByCountry(DateTime? beginningDate, DateTime? endingDate);
List<EmployeeSalesByCountryReturnModel> EmployeeSalesByCountry(DateTime? beginningDate, DateTime? endingDate, out int procResult);
Task<List<EmployeeSalesByCountryReturnModel>> EmployeeSalesByCountryAsync(DateTime? beginningDate, DateTime? endingDate);
List<SalesByCategoryReturnModel> SalesByCategory(string categoryName, string ordYear);
List<SalesByCategoryReturnModel> SalesByCategory(string categoryName, string ordYear, out int procResult);
Task<List<SalesByCategoryReturnModel>> SalesByCategoryAsync(string categoryName, string ordYear);
List<SalesByYearReturnModel> SalesByYear(DateTime? beginningDate, DateTime? endingDate);
List<SalesByYearReturnModel> SalesByYear(DateTime? beginningDate, DateTime? endingDate, out int procResult);
Task<List<SalesByYearReturnModel>> SalesByYearAsync(DateTime? beginningDate, DateTime? endingDate);
List<TenMostExpensiveProductsReturnModel> TenMostExpensiveProducts();
List<TenMostExpensiveProductsReturnModel> TenMostExpensiveProducts(out int procResult);
Task<List<TenMostExpensiveProductsReturnModel>> TenMostExpensiveProductsAsync();
}
#endregion
#region Database context
public class MyDbContext : DbContext, IMyDbContext
{
public MyDbContext()
{
}
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public DbSet<AlphabeticalListOfProduct> AlphabeticalListOfProducts { get; set; } // Alphabetical list of products
public DbSet<Category> Categories { get; set; } // Categories
public DbSet<CategorySalesFor1997> CategorySalesFor1997 { get; set; } // Category Sales for 1997
public DbSet<CurrentProductList> CurrentProductLists { get; set; } // Current Product List
public DbSet<Customer> Customers { get; set; } // Customers
public DbSet<CustomerAndSuppliersByCity> CustomerAndSuppliersByCities { get; set; } // Customer and Suppliers by City
public DbSet<CustomerCustomerDemo> CustomerCustomerDemoes { get; set; } // CustomerCustomerDemo
public DbSet<CustomerDemographic> CustomerDemographics { get; set; } // CustomerDemographics
public DbSet<Employee> Employees { get; set; } // Employees
public DbSet<EmployeeTerritory> EmployeeTerritories { get; set; } // EmployeeTerritories
public DbSet<Invoice> Invoices { get; set; } // Invoices
public DbSet<Order> Orders { get; set; } // Orders
public DbSet<OrderDetail> OrderDetails { get; set; } // Order Details
public DbSet<OrderDetailsExtended> OrderDetailsExtendeds { get; set; } // Order Details Extended
public DbSet<OrdersQry> OrdersQries { get; set; } // Orders Qry
public DbSet<OrderSubtotal> OrderSubtotals { get; set; } // Order Subtotals
public DbSet<Product> Products { get; set; } // Products
public DbSet<ProductsAboveAveragePrice> ProductsAboveAveragePrices { get; set; } // Products Above Average Price
public DbSet<ProductSalesFor1997> ProductSalesFor1997 { get; set; } // Product Sales for 1997
public DbSet<ProductsByCategory> ProductsByCategories { get; set; } // Products by Category
public DbSet<QuarterlyOrder> QuarterlyOrders { get; set; } // Quarterly Orders
public DbSet<Region> Regions { get; set; } // Region
public DbSet<SalesByCategory> SalesByCategories { get; set; } // Sales by Category
public DbSet<SalesTotalsByAmount> SalesTotalsByAmounts { get; set; } // Sales Totals by Amount
public DbSet<Shipper> Shippers { get; set; } // Shippers
public DbSet<SummaryOfSalesByQuarter> SummaryOfSalesByQuarters { get; set; } // Summary of Sales by Quarter
public DbSet<SummaryOfSalesByYear> SummaryOfSalesByYears { get; set; } // Summary of Sales by Year
public DbSet<Supplier> Suppliers { get; set; } // Suppliers
public DbSet<Territory> Territories { get; set; } // Territories
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Data Source=(local);Initial Catalog=Northwind;Integrated Security=True");
}
}
public bool IsSqlParameterNull(SqlParameter param)
{
var sqlValue = param.SqlValue;
var nullableValue = sqlValue as INullable;
if (nullableValue != null)
return nullableValue.IsNull;
return (sqlValue == null || sqlValue == DBNull.Value);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new AlphabeticalListOfProductConfiguration());
modelBuilder.ApplyConfiguration(new CategoryConfiguration());
modelBuilder.ApplyConfiguration(new CategorySalesFor1997Configuration());
modelBuilder.ApplyConfiguration(new CurrentProductListConfiguration());
modelBuilder.ApplyConfiguration(new CustomerConfiguration());
modelBuilder.ApplyConfiguration(new CustomerAndSuppliersByCityConfiguration());
modelBuilder.ApplyConfiguration(new CustomerCustomerDemoConfiguration());
modelBuilder.ApplyConfiguration(new CustomerDemographicConfiguration());
modelBuilder.ApplyConfiguration(new EmployeeConfiguration());
modelBuilder.ApplyConfiguration(new EmployeeTerritoryConfiguration());
modelBuilder.ApplyConfiguration(new InvoiceConfiguration());
modelBuilder.ApplyConfiguration(new OrderConfiguration());
modelBuilder.ApplyConfiguration(new OrderDetailConfiguration());
modelBuilder.ApplyConfiguration(new OrderDetailsExtendedConfiguration());
modelBuilder.ApplyConfiguration(new OrdersQryConfiguration());
modelBuilder.ApplyConfiguration(new OrderSubtotalConfiguration());
modelBuilder.ApplyConfiguration(new ProductConfiguration());
modelBuilder.ApplyConfiguration(new ProductsAboveAveragePriceConfiguration());
modelBuilder.ApplyConfiguration(new ProductSalesFor1997Configuration());
modelBuilder.ApplyConfiguration(new ProductsByCategoryConfiguration());
modelBuilder.ApplyConfiguration(new QuarterlyOrderConfiguration());
modelBuilder.ApplyConfiguration(new RegionConfiguration());
modelBuilder.ApplyConfiguration(new SalesByCategoryConfiguration());
modelBuilder.ApplyConfiguration(new SalesTotalsByAmountConfiguration());
modelBuilder.ApplyConfiguration(new ShipperConfiguration());
modelBuilder.ApplyConfiguration(new SummaryOfSalesByQuarterConfiguration());
modelBuilder.ApplyConfiguration(new SummaryOfSalesByYearConfiguration());
modelBuilder.ApplyConfiguration(new SupplierConfiguration());
modelBuilder.ApplyConfiguration(new TerritoryConfiguration());
modelBuilder.Entity<CustOrderHistReturnModel>().HasNoKey();
modelBuilder.Entity<CustOrdersDetailReturnModel>().HasNoKey();
modelBuilder.Entity<CustOrdersOrdersReturnModel>().HasNoKey();
modelBuilder.Entity<EmployeeSalesByCountryReturnModel>().HasNoKey();
modelBuilder.Entity<SalesByCategoryReturnModel>().HasNoKey();
modelBuilder.Entity<SalesByYearReturnModel>().HasNoKey();
modelBuilder.Entity<TenMostExpensiveProductsReturnModel>().HasNoKey();
}
// Stored Procedures
public List<CustOrderHistReturnModel> CustOrderHist(string customerId)
{
int procResult;
return CustOrderHist(customerId, out procResult);
}
public List<CustOrderHistReturnModel> CustOrderHist(string customerId, out int procResult)
{
var customerIdParam = new SqlParameter { ParameterName = "@CustomerID", SqlDbType = SqlDbType.NChar, Direction = ParameterDirection.Input, Value = customerId, Size = 5 };
if (customerIdParam.Value == null)
customerIdParam.Value = DBNull.Value;
var procResultParam = new SqlParameter { ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output };
const string sqlCommand = "EXEC @procResult = [dbo].[CustOrderHist] @CustomerID";
var procResultData = Set<CustOrderHistReturnModel>()
.FromSqlRaw(sqlCommand, customerIdParam, procResultParam)
.ToList();
procResult = (int) procResultParam.Value;
return procResultData;
}
public async Task<List<CustOrderHistReturnModel>> CustOrderHistAsync(string customerId)
{
var customerIdParam = new SqlParameter { ParameterName = "@CustomerID", SqlDbType = SqlDbType.NChar, Direction = ParameterDirection.Input, Value = customerId, Size = 5 };
if (customerIdParam.Value == null)
customerIdParam.Value = DBNull.Value;
const string sqlCommand = "EXEC [dbo].[CustOrderHist] @CustomerID";
var procResultData = await Set<CustOrderHistReturnModel>()
.FromSqlRaw(sqlCommand, customerIdParam)
.ToListAsync();
return procResultData;
}
public List<CustOrdersDetailReturnModel> CustOrdersDetail(int? orderId)
{
int procResult;
return CustOrdersDetail(orderId, out procResult);
}
public List<CustOrdersDetailReturnModel> CustOrdersDetail(int? orderId, out int procResult)
{
var orderIdParam = new SqlParameter { ParameterName = "@OrderID", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Input, Value = orderId.GetValueOrDefault(), Precision = 10, Scale = 0 };
if (!orderId.HasValue)
orderIdParam.Value = DBNull.Value;
var procResultParam = new SqlParameter { ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output };
const string sqlCommand = "EXEC @procResult = [dbo].[CustOrdersDetail] @OrderID";
var procResultData = Set<CustOrdersDetailReturnModel>()
.FromSqlRaw(sqlCommand, orderIdParam, procResultParam)
.ToList();
procResult = (int) procResultParam.Value;
return procResultData;
}
public async Task<List<CustOrdersDetailReturnModel>> CustOrdersDetailAsync(int? orderId)
{
var orderIdParam = new SqlParameter { ParameterName = "@OrderID", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Input, Value = orderId.GetValueOrDefault(), Precision = 10, Scale = 0 };
if (!orderId.HasValue)
orderIdParam.Value = DBNull.Value;
const string sqlCommand = "EXEC [dbo].[CustOrdersDetail] @OrderID";
var procResultData = await Set<CustOrdersDetailReturnModel>()
.FromSqlRaw(sqlCommand, orderIdParam)
.ToListAsync();
return procResultData;
}
public List<CustOrdersOrdersReturnModel> CustOrdersOrders(string customerId)
{
int procResult;
return CustOrdersOrders(customerId, out procResult);
}
public List<CustOrdersOrdersReturnModel> CustOrdersOrders(string customerId, out int procResult)
{
var customerIdParam = new SqlParameter { ParameterName = "@CustomerID", SqlDbType = SqlDbType.NChar, Direction = ParameterDirection.Input, Value = customerId, Size = 5 };
if (customerIdParam.Value == null)
customerIdParam.Value = DBNull.Value;
var procResultParam = new SqlParameter { ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output };
const string sqlCommand = "EXEC @procResult = [dbo].[CustOrdersOrders] @CustomerID";
var procResultData = Set<CustOrdersOrdersReturnModel>()
.FromSqlRaw(sqlCommand, customerIdParam, procResultParam)
.ToList();
procResult = (int) procResultParam.Value;
return procResultData;
}
public async Task<List<CustOrdersOrdersReturnModel>> CustOrdersOrdersAsync(string customerId)
{
var customerIdParam = new SqlParameter { ParameterName = "@CustomerID", SqlDbType = SqlDbType.NChar, Direction = ParameterDirection.Input, Value = customerId, Size = 5 };
if (customerIdParam.Value == null)
customerIdParam.Value = DBNull.Value;
const string sqlCommand = "EXEC [dbo].[CustOrdersOrders] @CustomerID";
var procResultData = await Set<CustOrdersOrdersReturnModel>()
.FromSqlRaw(sqlCommand, customerIdParam)
.ToListAsync();
return procResultData;
}
public List<EmployeeSalesByCountryReturnModel> EmployeeSalesByCountry(DateTime? beginningDate, DateTime? endingDate)
{
int procResult;
return EmployeeSalesByCountry(beginningDate, endingDate, out procResult);
}
public List<EmployeeSalesByCountryReturnModel> EmployeeSalesByCountry(DateTime? beginningDate, DateTime? endingDate, out int procResult)
{
var beginningDateParam = new SqlParameter { ParameterName = "@Beginning_Date", SqlDbType = SqlDbType.DateTime, Direction = ParameterDirection.Input, Value = beginningDate.GetValueOrDefault() };
if (!beginningDate.HasValue)
beginningDateParam.Value = DBNull.Value;
var endingDateParam = new SqlParameter { ParameterName = "@Ending_Date", SqlDbType = SqlDbType.DateTime, Direction = ParameterDirection.Input, Value = endingDate.GetValueOrDefault() };
if (!endingDate.HasValue)
endingDateParam.Value = DBNull.Value;
var procResultParam = new SqlParameter { ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output };
const string sqlCommand = "EXEC @procResult = [dbo].[Employee Sales by Country] @Beginning_Date, @Ending_Date";
var procResultData = Set<EmployeeSalesByCountryReturnModel>()
.FromSqlRaw(sqlCommand, beginningDateParam, endingDateParam, procResultParam)
.ToList();
procResult = (int) procResultParam.Value;
return procResultData;
}
public async Task<List<EmployeeSalesByCountryReturnModel>> EmployeeSalesByCountryAsync(DateTime? beginningDate, DateTime? endingDate)
{
var beginningDateParam = new SqlParameter { ParameterName = "@Beginning_Date", SqlDbType = SqlDbType.DateTime, Direction = ParameterDirection.Input, Value = beginningDate.GetValueOrDefault() };
if (!beginningDate.HasValue)
beginningDateParam.Value = DBNull.Value;
var endingDateParam = new SqlParameter { ParameterName = "@Ending_Date", SqlDbType = SqlDbType.DateTime, Direction = ParameterDirection.Input, Value = endingDate.GetValueOrDefault() };
if (!endingDate.HasValue)
endingDateParam.Value = DBNull.Value;
const string sqlCommand = "EXEC [dbo].[Employee Sales by Country] @Beginning_Date, @Ending_Date";
var procResultData = await Set<EmployeeSalesByCountryReturnModel>()
.FromSqlRaw(sqlCommand, beginningDateParam, endingDateParam)
.ToListAsync();
return procResultData;
}
public List<SalesByCategoryReturnModel> SalesByCategory(string categoryName, string ordYear)
{
int procResult;
return SalesByCategory(categoryName, ordYear, out procResult);
}
public List<SalesByCategoryReturnModel> SalesByCategory(string categoryName, string ordYear, out int procResult)
{
var categoryNameParam = new SqlParameter { ParameterName = "@CategoryName", SqlDbType = SqlDbType.NVarChar, Direction = ParameterDirection.Input, Value = categoryName, Size = 15 };
if (categoryNameParam.Value == null)
categoryNameParam.Value = DBNull.Value;
var ordYearParam = new SqlParameter { ParameterName = "@OrdYear", SqlDbType = SqlDbType.NVarChar, Direction = ParameterDirection.Input, Value = ordYear, Size = 4 };
if (ordYearParam.Value == null)
ordYearParam.Value = DBNull.Value;
var procResultParam = new SqlParameter { ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output };
const string sqlCommand = "EXEC @procResult = [dbo].[SalesByCategory] @CategoryName, @OrdYear";
var procResultData = Set<SalesByCategoryReturnModel>()
.FromSqlRaw(sqlCommand, categoryNameParam, ordYearParam, procResultParam)
.ToList();
procResult = (int) procResultParam.Value;
return procResultData;
}
public async Task<List<SalesByCategoryReturnModel>> SalesByCategoryAsync(string categoryName, string ordYear)
{
var categoryNameParam = new SqlParameter { ParameterName = "@CategoryName", SqlDbType = SqlDbType.NVarChar, Direction = ParameterDirection.Input, Value = categoryName, Size = 15 };
if (categoryNameParam.Value == null)
categoryNameParam.Value = DBNull.Value;
var ordYearParam = new SqlParameter { ParameterName = "@OrdYear", SqlDbType = SqlDbType.NVarChar, Direction = ParameterDirection.Input, Value = ordYear, Size = 4 };
if (ordYearParam.Value == null)
ordYearParam.Value = DBNull.Value;
const string sqlCommand = "EXEC [dbo].[SalesByCategory] @CategoryName, @OrdYear";
var procResultData = await Set<SalesByCategoryReturnModel>()
.FromSqlRaw(sqlCommand, categoryNameParam, ordYearParam)
.ToListAsync();
return procResultData;
}
public List<SalesByYearReturnModel> SalesByYear(DateTime? beginningDate, DateTime? endingDate)
{
int procResult;
return SalesByYear(beginningDate, endingDate, out procResult);
}
public List<SalesByYearReturnModel> SalesByYear(DateTime? beginningDate, DateTime? endingDate, out int procResult)
{
var beginningDateParam = new SqlParameter { ParameterName = "@Beginning_Date", SqlDbType = SqlDbType.DateTime, Direction = ParameterDirection.Input, Value = beginningDate.GetValueOrDefault() };
if (!beginningDate.HasValue)
beginningDateParam.Value = DBNull.Value;
var endingDateParam = new SqlParameter { ParameterName = "@Ending_Date", SqlDbType = SqlDbType.DateTime, Direction = ParameterDirection.Input, Value = endingDate.GetValueOrDefault() };
if (!endingDate.HasValue)
endingDateParam.Value = DBNull.Value;
var procResultParam = new SqlParameter { ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output };
const string sqlCommand = "EXEC @procResult = [dbo].[Sales by Year] @Beginning_Date, @Ending_Date";
var procResultData = Set<SalesByYearReturnModel>()
.FromSqlRaw(sqlCommand, beginningDateParam, endingDateParam, procResultParam)
.ToList();
procResult = (int) procResultParam.Value;
return procResultData;
}
public async Task<List<SalesByYearReturnModel>> SalesByYearAsync(DateTime? beginningDate, DateTime? endingDate)
{
var beginningDateParam = new SqlParameter { ParameterName = "@Beginning_Date", SqlDbType = SqlDbType.DateTime, Direction = ParameterDirection.Input, Value = beginningDate.GetValueOrDefault() };
if (!beginningDate.HasValue)
beginningDateParam.Value = DBNull.Value;
var endingDateParam = new SqlParameter { ParameterName = "@Ending_Date", SqlDbType = SqlDbType.DateTime, Direction = ParameterDirection.Input, Value = endingDate.GetValueOrDefault() };
if (!endingDate.HasValue)
endingDateParam.Value = DBNull.Value;
const string sqlCommand = "EXEC [dbo].[Sales by Year] @Beginning_Date, @Ending_Date";
var procResultData = await Set<SalesByYearReturnModel>()
.FromSqlRaw(sqlCommand, beginningDateParam, endingDateParam)
.ToListAsync();
return procResultData;
}
public List<TenMostExpensiveProductsReturnModel> TenMostExpensiveProducts()
{
int procResult;
return TenMostExpensiveProducts(out procResult);
}
public List<TenMostExpensiveProductsReturnModel> TenMostExpensiveProducts(out int procResult)
{
var procResultParam = new SqlParameter { ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output };
const string sqlCommand = "EXEC @procResult = [dbo].[Ten Most Expensive Products]";
var procResultData = Set<TenMostExpensiveProductsReturnModel>()
.FromSqlRaw(sqlCommand, procResultParam)
.ToList();
procResult = (int) procResultParam.Value;
return procResultData;
}
public async Task<List<TenMostExpensiveProductsReturnModel>> TenMostExpensiveProductsAsync()
{
const string sqlCommand = "EXEC [dbo].[Ten Most Expensive Products]";
var procResultData = await Set<TenMostExpensiveProductsReturnModel>()
.FromSqlRaw(sqlCommand)
.ToListAsync();
return procResultData;
}
}
#endregion
#region Database context factory
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
public MyDbContext CreateDbContext(string[] args)
{
return new MyDbContext();
}
}
#endregion
#region Fake Database context
public class FakeMyDbContext : IMyDbContext
{
public DbSet<AlphabeticalListOfProduct> AlphabeticalListOfProducts { get; set; } // Alphabetical list of products
public DbSet<Category> Categories { get; set; } // Categories
public DbSet<CategorySalesFor1997> CategorySalesFor1997 { get; set; } // Category Sales for 1997
public DbSet<CurrentProductList> CurrentProductLists { get; set; } // Current Product List
public DbSet<Customer> Customers { get; set; } // Customers
public DbSet<CustomerAndSuppliersByCity> CustomerAndSuppliersByCities { get; set; } // Customer and Suppliers by City
public DbSet<CustomerCustomerDemo> CustomerCustomerDemoes { get; set; } // CustomerCustomerDemo
public DbSet<CustomerDemographic> CustomerDemographics { get; set; } // CustomerDemographics
public DbSet<Employee> Employees { get; set; } // Employees
public DbSet<EmployeeTerritory> EmployeeTerritories { get; set; } // EmployeeTerritories
public DbSet<Invoice> Invoices { get; set; } // Invoices
public DbSet<Order> Orders { get; set; } // Orders
public DbSet<OrderDetail> OrderDetails { get; set; } // Order Details
public DbSet<OrderDetailsExtended> OrderDetailsExtendeds { get; set; } // Order Details Extended
public DbSet<OrdersQry> OrdersQries { get; set; } // Orders Qry
public DbSet<OrderSubtotal> OrderSubtotals { get; set; } // Order Subtotals
public DbSet<Product> Products { get; set; } // Products
public DbSet<ProductsAboveAveragePrice> ProductsAboveAveragePrices { get; set; } // Products Above Average Price
public DbSet<ProductSalesFor1997> ProductSalesFor1997 { get; set; } // Product Sales for 1997
public DbSet<ProductsByCategory> ProductsByCategories { get; set; } // Products by Category
public DbSet<QuarterlyOrder> QuarterlyOrders { get; set; } // Quarterly Orders
public DbSet<Region> Regions { get; set; } // Region
public DbSet<SalesByCategory> SalesByCategories { get; set; } // Sales by Category
public DbSet<SalesTotalsByAmount> SalesTotalsByAmounts { get; set; } // Sales Totals by Amount
public DbSet<Shipper> Shippers { get; set; } // Shippers
public DbSet<SummaryOfSalesByQuarter> SummaryOfSalesByQuarters { get; set; } // Summary of Sales by Quarter
public DbSet<SummaryOfSalesByYear> SummaryOfSalesByYears { get; set; } // Summary of Sales by Year
public DbSet<Supplier> Suppliers { get; set; } // Suppliers
public DbSet<Territory> Territories { get; set; } // Territories
public FakeMyDbContext()
{
_database = null;
AlphabeticalListOfProducts = new FakeDbSet<AlphabeticalListOfProduct>();
Categories = new FakeDbSet<Category>("CategoryId");
CategorySalesFor1997 = new FakeDbSet<CategorySalesFor1997>();
CurrentProductLists = new FakeDbSet<CurrentProductList>();
Customers = new FakeDbSet<Customer>("CustomerId");
CustomerAndSuppliersByCities = new FakeDbSet<CustomerAndSuppliersByCity>();
CustomerCustomerDemoes = new FakeDbSet<CustomerCustomerDemo>("CustomerId", "CustomerTypeId");
CustomerDemographics = new FakeDbSet<CustomerDemographic>("CustomerTypeId");
Employees = new FakeDbSet<Employee>("EmployeeId");
EmployeeTerritories = new FakeDbSet<EmployeeTerritory>("EmployeeId", "TerritoryId");
Invoices = new FakeDbSet<Invoice>();
Orders = new FakeDbSet<Order>("OrderId");
OrderDetails = new FakeDbSet<OrderDetail>("OrderId", "ProductId");
OrderDetailsExtendeds = new FakeDbSet<OrderDetailsExtended>();
OrdersQries = new FakeDbSet<OrdersQry>();
OrderSubtotals = new FakeDbSet<OrderSubtotal>();
Products = new FakeDbSet<Product>("ProductId");
ProductsAboveAveragePrices = new FakeDbSet<ProductsAboveAveragePrice>();
ProductSalesFor1997 = new FakeDbSet<ProductSalesFor1997>();
ProductsByCategories = new FakeDbSet<ProductsByCategory>();
QuarterlyOrders = new FakeDbSet<QuarterlyOrder>();
Regions = new FakeDbSet<Region>("RegionId");
SalesByCategories = new FakeDbSet<SalesByCategory>();
SalesTotalsByAmounts = new FakeDbSet<SalesTotalsByAmount>();
Shippers = new FakeDbSet<Shipper>("ShipperId");
SummaryOfSalesByQuarters = new FakeDbSet<SummaryOfSalesByQuarter>();
SummaryOfSalesByYears = new FakeDbSet<SummaryOfSalesByYear>();
Suppliers = new FakeDbSet<Supplier>("SupplierId");
Territories = new FakeDbSet<Territory>("TerritoryId");
}
public int SaveChangesCount { get; private set; }
public virtual int SaveChanges()
{
++SaveChangesCount;
return 1;
}
public virtual int SaveChanges(bool acceptAllChangesOnSuccess)
{
return SaveChanges();
}
public virtual Task<int> SaveChangesAsync(CancellationToken cancellationToken)
{
++SaveChangesCount;
return Task<int>.Factory.StartNew(() => 1, cancellationToken);
}
public virtual Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken)
{
++SaveChangesCount;
return Task<int>.Factory.StartNew(x => 1, acceptAllChangesOnSuccess, cancellationToken);
}
protected virtual void Dispose(bool disposing)
{
}
public void Dispose()
{
Dispose(true);
}
private DatabaseFacade _database;
public DatabaseFacade Database { get { return _database; } }
public DbSet<TEntity> Set<TEntity>() where TEntity : class
{
throw new NotImplementedException();
}
public override string ToString()
{
throw new NotImplementedException();
}
// Stored Procedures
public DbSet<CustOrderHistReturnModel> CustOrderHistReturnModel { get; set; }
public List<CustOrderHistReturnModel> CustOrderHist(string customerId)
{
int procResult;
return CustOrderHist(customerId, out procResult);
}
public List<CustOrderHistReturnModel> CustOrderHist(string customerId, out int procResult)
{
procResult = 0;
return new List<CustOrderHistReturnModel>();
}
public Task<List<CustOrderHistReturnModel>> CustOrderHistAsync(string customerId)
{
int procResult;
return Task.FromResult(CustOrderHist(customerId, out procResult));
}
public DbSet<CustOrdersDetailReturnModel> CustOrdersDetailReturnModel { get; set; }
public List<CustOrdersDetailReturnModel> CustOrdersDetail(int? orderId)
{
int procResult;
return CustOrdersDetail(orderId, out procResult);
}
public List<CustOrdersDetailReturnModel> CustOrdersDetail(int? orderId, out int procResult)
{
procResult = 0;
return new List<CustOrdersDetailReturnModel>();
}
public Task<List<CustOrdersDetailReturnModel>> CustOrdersDetailAsync(int? orderId)
{
int procResult;
return Task.FromResult(CustOrdersDetail(orderId, out procResult));
}
public DbSet<CustOrdersOrdersReturnModel> CustOrdersOrdersReturnModel { get; set; }
public List<CustOrdersOrdersReturnModel> CustOrdersOrders(string customerId)
{
int procResult;
return CustOrdersOrders(customerId, out procResult);
}
public List<CustOrdersOrdersReturnModel> CustOrdersOrders(string customerId, out int procResult)
{
procResult = 0;
return new List<CustOrdersOrdersReturnModel>();
}
public Task<List<CustOrdersOrdersReturnModel>> CustOrdersOrdersAsync(string customerId)
{
int procResult;
return Task.FromResult(CustOrdersOrders(customerId, out procResult));
}
public DbSet<EmployeeSalesByCountryReturnModel> EmployeeSalesByCountryReturnModel { get; set; }
public List<EmployeeSalesByCountryReturnModel> EmployeeSalesByCountry(DateTime? beginningDate, DateTime? endingDate)
{
int procResult;
return EmployeeSalesByCountry(beginningDate, endingDate, out procResult);
}
public List<EmployeeSalesByCountryReturnModel> EmployeeSalesByCountry(DateTime? beginningDate, DateTime? endingDate, out int procResult)
{
procResult = 0;
return new List<EmployeeSalesByCountryReturnModel>();
}
public Task<List<EmployeeSalesByCountryReturnModel>> EmployeeSalesByCountryAsync(DateTime? beginningDate, DateTime? endingDate)
{
int procResult;
return Task.FromResult(EmployeeSalesByCountry(beginningDate, endingDate, out procResult));
}
public DbSet<SalesByCategoryReturnModel> SalesByCategoryReturnModel { get; set; }
public List<SalesByCategoryReturnModel> SalesByCategory(string categoryName, string ordYear)
{
int procResult;
return SalesByCategory(categoryName, ordYear, out procResult);
}
public List<SalesByCategoryReturnModel> SalesByCategory(string categoryName, string ordYear, out int procResult)
{
procResult = 0;
return new List<SalesByCategoryReturnModel>();
}
public Task<List<SalesByCategoryReturnModel>> SalesByCategoryAsync(string categoryName, string ordYear)
{
int procResult;
return Task.FromResult(SalesByCategory(categoryName, ordYear, out procResult));
}
public DbSet<SalesByYearReturnModel> SalesByYearReturnModel { get; set; }
public List<SalesByYearReturnModel> SalesByYear(DateTime? beginningDate, DateTime? endingDate)
{
int procResult;
return SalesByYear(beginningDate, endingDate, out procResult);
}
public List<SalesByYearReturnModel> SalesByYear(DateTime? beginningDate, DateTime? endingDate, out int procResult)
{
procResult = 0;
return new List<SalesByYearReturnModel>();
}
public Task<List<SalesByYearReturnModel>> SalesByYearAsync(DateTime? beginningDate, DateTime? endingDate)
{
int procResult;
return Task.FromResult(SalesByYear(beginningDate, endingDate, out procResult));
}
public DbSet<TenMostExpensiveProductsReturnModel> TenMostExpensiveProductsReturnModel { get; set; }
public List<TenMostExpensiveProductsReturnModel> TenMostExpensiveProducts()
{
int procResult;
return TenMostExpensiveProducts(out procResult);
}
public List<TenMostExpensiveProductsReturnModel> TenMostExpensiveProducts(out int procResult)
{
procResult = 0;
return new List<TenMostExpensiveProductsReturnModel>();
}
public Task<List<TenMostExpensiveProductsReturnModel>> TenMostExpensiveProductsAsync()
{
int procResult;
return Task.FromResult(TenMostExpensiveProducts(out procResult));
}
}
#endregion
#region Fake DbSet
// ************************************************************************
// Fake DbSet
// Implementing Find:
// The Find method is difficult to implement in a generic fashion. If
// you need to test code that makes use of the Find method it is
// easiest to create a test DbSet for each of the entity types that
// need to support find. You can then write logic to find that
// particular type of entity, as shown below:
// public class FakeBlogDbSet : FakeDbSet<Blog>
// {
// public override Blog Find(params object[] keyValues)
// {
// var id = (int) keyValues.Single();
// return this.SingleOrDefault(b => b.BlogId == id);
// }
// }
// Read more about it here: https://msdn.microsoft.com/en-us/data/dn314431.aspx
public class FakeDbSet<TEntity> : DbSet<TEntity>, IQueryable<TEntity>, IAsyncEnumerable<TEntity>, IListSource where TEntity : class
{
private readonly PropertyInfo[] _primaryKeys;
private readonly ObservableCollection<TEntity> _data;
private readonly IQueryable _query;
public FakeDbSet()
{
_primaryKeys = null;
_data = new ObservableCollection<TEntity>();
_query = _data.AsQueryable();
}
public FakeDbSet(params string[] primaryKeys)
{
_primaryKeys = typeof(TEntity).GetProperties().Where(x => primaryKeys.Contains(x.Name)).ToArray();
_data = new ObservableCollection<TEntity>();
_query = _data.AsQueryable();
}
public override TEntity Find(params object[] keyValues)
{
if (_primaryKeys == null)
throw new ArgumentException("No primary keys defined");
if (keyValues.Length != _primaryKeys.Length)
throw new ArgumentException("Incorrect number of keys passed to Find method");
var keyQuery = this.AsQueryable();
keyQuery = keyValues
.Select((t, i) => i)
.Aggregate(keyQuery,
(current, x) =>
current.Where(entity => _primaryKeys[x].GetValue(entity, null).Equals(keyValues[x])));
return keyQuery.SingleOrDefault();
}
public override ValueTask<TEntity> FindAsync(object[] keyValues, CancellationToken cancellationToken)
{
return new ValueTask<TEntity>(Task<TEntity>.Factory.StartNew(() => Find(keyValues), cancellationToken));
}
public override ValueTask<TEntity> FindAsync(params object[] keyValues)
{
return new ValueTask<TEntity>(Task<TEntity>.Factory.StartNew(() => Find(keyValues)));
}
IAsyncEnumerator<TEntity> IAsyncEnumerable<TEntity>.GetAsyncEnumerator(CancellationToken cancellationToken)
{
return GetAsyncEnumerator(cancellationToken);
}
public override EntityEntry<TEntity> Add(TEntity entity)
{
_data.Add(entity);
return null;
}
public override void AddRange(params TEntity[] entities)
{
if (entities == null) throw new ArgumentNullException("entities");
foreach (var entity in entities.ToList())
_data.Add(entity);
}
public override void AddRange(IEnumerable<TEntity> entities)
{
AddRange(entities.ToArray());
}
public override Task AddRangeAsync(params TEntity[] entities)
{
if (entities == null) throw new ArgumentNullException("entities");
return Task.Factory.StartNew(() => AddRange(entities));
}
public override void AttachRange(params TEntity[] entities)
{
if (entities == null) throw new ArgumentNullException("entities");
AddRange(entities);
}
public override void RemoveRange(params TEntity[] entities)
{
if (entities == null) throw new ArgumentNullException("entities");
foreach (var entity in entities.ToList())
_data.Remove(entity);
}
public override void RemoveRange(IEnumerable<TEntity> entities)
{
RemoveRange(entities.ToArray());
}
public override void UpdateRange(params TEntity[] entities)
{
if (entities == null) throw new ArgumentNullException("entities");
RemoveRange(entities);
AddRange(entities);
}
public IList GetList()
{
return _data;
}
IList IListSource.GetList()
{
return _data;
}
Type IQueryable.ElementType
{
get { return _query.ElementType; }
}
Expression IQueryable.Expression
{
get { return _query.Expression; }
}
IQueryProvider IQueryable.Provider
{
get { return new FakeDbAsyncQueryProvider<TEntity>(_query.Provider); }
}
IEnumerator IEnumerable.GetEnumerator()
{
return _data.GetEnumerator();
}
IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
{
return _data.GetEnumerator();
}
IAsyncEnumerator<TEntity> GetAsyncEnumerator(CancellationToken cancellationToken = default(CancellationToken))
{
return new FakeDbAsyncEnumerator<TEntity>(this.AsEnumerable().GetEnumerator());
}
}
public class FakeDbAsyncQueryProvider<TEntity> : IAsyncQueryProvider
{
private readonly IQueryProvider _inner;
public FakeDbAsyncQueryProvider(IQueryProvider inner)
{
_inner = inner;
}
public IQueryable CreateQuery(Expression expression)
{
var m = expression as MethodCallExpression;
if (m != null)
{
var resultType = m.Method.ReturnType; // it should be IQueryable<T>
var tElement = resultType.GetGenericArguments()[0];
var queryType = typeof(FakeDbAsyncEnumerable<>).MakeGenericType(tElement);
return (IQueryable) Activator.CreateInstance(queryType, expression);
}
return new FakeDbAsyncEnumerable<TEntity>(expression);
}
public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
var queryType = typeof(FakeDbAsyncEnumerable<>).MakeGenericType(typeof(TElement));
return (IQueryable<TElement>) Activator.CreateInstance(queryType, expression);
}
public object Execute(Expression expression)
{
return _inner.Execute(expression);
}
public TResult Execute<TResult>(Expression expression)
{
return _inner.Execute<TResult>(expression);
}
public TResult ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken = new CancellationToken())
{
return _inner.Execute<TResult>(expression);
}
}
public class FakeDbAsyncEnumerable<T> : EnumerableQuery<T>, IAsyncEnumerable<T>, IQueryable<T>
{
public FakeDbAsyncEnumerable(IEnumerable<T> enumerable)
: base(enumerable)
{
}
public FakeDbAsyncEnumerable(Expression expression)
: base(expression)
{
}
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken())
{
return new FakeDbAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
}
IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator(CancellationToken cancellationToken)
{