Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ObjectContext.cs « Objects « Data « System « System.Data.Entity « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49b58de7cd196d9124981f1619707bcb1051acfd (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
//---------------------------------------------------------------------
// <copyright file="ObjectContext.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------

namespace System.Data.Objects
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration;
    using System.Data.Common;
    using System.Data.Common.CommandTrees;
    using System.Data.Common.CommandTrees.ExpressionBuilder;
    using System.Data.Common.Internal.Materialization;
    using System.Data.Common.Utils;
    using System.Data.Entity;
    using System.Data.EntityClient;
    using System.Data.Metadata.Edm;
    using System.Data.Objects.DataClasses;
    using System.Data.Objects.ELinq;
    using System.Data.Objects.Internal;
    using System.Data.Query.InternalTrees;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Runtime.Versioning;
    using System.Text;
    using System.Transactions;

    /// <summary>
    /// Defines options that affect the behavior of the ObjectContext.
    /// </summary>
    public sealed class ObjectContextOptions
    {
        private bool _lazyLoadingEnabled;
        private bool _proxyCreationEnabled = true;
        private bool _useLegacyPreserveChangesBehavior = false;
        private bool _useConsistentNullReferenceBehavior;
        private bool _useCSharpNullComparisonBehavior = false;

        internal ObjectContextOptions()
        {
        }

        /// <summary>
        /// Get or set boolean that determines if related ends can be loaded on demand 
        /// when they are accessed through a navigation property.
        /// </summary>
        /// <value>
        /// True if related ends can be loaded on demand; otherwise false.
        /// </value>
        public bool LazyLoadingEnabled
        {
            get { return _lazyLoadingEnabled; }
            set { _lazyLoadingEnabled = value; }
        }

        /// <summary>
        /// Get or set boolean that determines whether proxy instances will be create
        /// for CLR types with a corresponding proxy type.
        /// </summary>
        /// <value>
        /// True if proxy instances should be created; otherwise false to create "normal" instances of the type.
        /// </value>
        public bool ProxyCreationEnabled
        {
            get { return _proxyCreationEnabled; }
            set { _proxyCreationEnabled = value; }
        }

        /// <summary>
        /// Get or set a boolean that determines whether to use the legacy MergeOption.PreserveChanges behavior
        /// when querying for entities using MergeOption.PreserveChanges
        /// </summary>
        /// <value>
        /// True if the legacy MergeOption.PreserveChanges behavior should be used; otherwise false.
        /// </value>
        public bool UseLegacyPreserveChangesBehavior
        {
            get { return _useLegacyPreserveChangesBehavior; }
            set { _useLegacyPreserveChangesBehavior = value; }
        }

        /// <summary>
        /// If this flag is set to false then setting the Value property of the <see cref="EntityReference{T}"/> for an
        /// FK relationship to null when it is already null will have no effect. When this flag is set to true, then
        /// setting the value to null will always cause the FK to be nulled and the relationship to be deleted
        /// even if the value is currently null. The default value is false when using ObjectContext and true
        /// when using DbContext.
        /// </summary>
        public bool UseConsistentNullReferenceBehavior
        {
            get { return _useConsistentNullReferenceBehavior; }
            set { _useConsistentNullReferenceBehavior = value; }
        }

        /// <summary>
        /// This flag determines whether C# behavior should be exhibited when comparing null values in LinqToEntities. 
        /// If this flag is set, then any equality comparison between two operands, both of which are potentially 
        /// nullable, will be rewritten to show C# null comparison semantics. As an example: 
        /// (operand1 = operand2) will be rewritten as 
        /// (((operand1 = operand2) AND NOT (operand1 IS NULL OR operand2 IS NULL)) || (operand1 IS NULL && operand2 IS NULL))
        /// The default value is false when using <see cref="ObjectContext"/>.
        /// </summary>
        public bool UseCSharpNullComparisonBehavior
        {
            get { return _useCSharpNullComparisonBehavior; }
            set { _useCSharpNullComparisonBehavior = value; }
        }
    }

    /// <summary>
    /// ObjectContext is the top-level object that encapsulates a connection between the CLR and the database,
    /// serving as a gateway for Create, Read, Update, and Delete operations.
    /// </summary>
    public class ObjectContext : IDisposable
    {
        #region Fields
        private IEntityAdapter _adapter;

        // Connection may be null if used by ObjectMaterializer for detached ObjectContext,
        // but those code paths should not touch the connection.
        //
        // If the connection is null, this indicates that this object has been disposed.
        // Disposal for this class doesn't mean complete disposal, 
        // but rather the disposal of the underlying connection object if the ObjectContext owns the connection,
        // or the separation of the underlying connection object from the ObjectContext if the ObjectContext does not own the connection.
        //
        // Operations that require a connection should throw an ObjectDiposedException if the connection is null.
        // Other operations that do not need a connection should continue to work after disposal.
        private EntityConnection _connection;

        private readonly MetadataWorkspace _workspace;
        private ObjectStateManager _cache;
        private ClrPerspective _perspective;
        private readonly bool _createdConnection;
        private bool _openedConnection;             // whether or not the context opened the connection to do an operation
        private int _connectionRequestCount;        // the number of active requests for an open connection
        private int? _queryTimeout;
        private Transaction _lastTransaction;

        private bool _disallowSettingDefaultContainerName;

        private EventHandler _onSavingChanges;

        private ObjectMaterializedEventHandler _onObjectMaterialized;

        private ObjectQueryProvider _queryProvider;

        private readonly ObjectContextOptions _options = new ObjectContextOptions();

        private readonly string s_UseLegacyPreserveChangesBehavior = "EntityFramework_UseLegacyPreserveChangesBehavior";

        #endregion Fields

        #region Constructors
        /// <summary>
        /// Creates an ObjectContext with the given connection and metadata workspace.
        /// </summary>
        /// <param name="connection">connection to the store</param>
        public ObjectContext(EntityConnection connection)
            : this(EntityUtil.CheckArgumentNull(connection, "connection"), true)
        {
        }

        /// <summary>
        /// Creates an ObjectContext with the given connection string and
        /// default entity container name.  This constructor
        /// creates and initializes an EntityConnection so that the context is
        /// ready to use; no other initialization is necessary.  The given
        /// connection string must be valid for an EntityConnection; connection
        /// strings for other connection types are not supported.
        /// </summary>
        /// <param name="connectionString">the connection string to use in the underlying EntityConnection to the store</param>
        /// <exception cref="ArgumentNullException">connectionString is null</exception>
        /// <exception cref="ArgumentException">if connectionString is invalid</exception>
        [ResourceExposure(ResourceScope.Machine)] //Exposes the file names as part of ConnectionString which are a Machine resource
        [ResourceConsumption(ResourceScope.Machine)] //For CreateEntityConnection method. But the paths are not created in this method.
        public ObjectContext(string connectionString)
            : this(CreateEntityConnection(connectionString), false)
        {
            _createdConnection = true;
        }


        /// <summary>
        /// Creates an ObjectContext with the given connection string and
        /// default entity container name.  This protected constructor creates and initializes an EntityConnection so that the context 
        /// is ready to use; no other initialization is necessary.  The given connection string must be valid for an EntityConnection; 
        /// connection strings for other connection types are not supported.
        /// </summary>
        /// <param name="connectionString">the connection string to use in the underlying EntityConnection to the store</param>
        /// <param name="defaultContainerName">the name of the default entity container</param>
        /// <exception cref="ArgumentNullException">connectionString is null</exception>
        /// <exception cref="ArgumentException">either connectionString or defaultContainerName is invalid</exception>
        [ResourceExposure(ResourceScope.Machine)] //Exposes the file names as part of ConnectionString which are a Machine resource
        [ResourceConsumption(ResourceScope.Machine)] //For ObjectContext method. But the paths are not created in this method.
        protected ObjectContext(string connectionString, string defaultContainerName)
            : this(connectionString)
        {
            DefaultContainerName = defaultContainerName;
            if (!string.IsNullOrEmpty(defaultContainerName))
            {
                _disallowSettingDefaultContainerName = true;
            }
        }

        /// <summary>
        /// Creates an ObjectContext with the given connection and metadata workspace.
        /// </summary>
        /// <param name="connection">connection to the store</param>
        /// <param name="defaultContainerName">the name of the default entity container</param>
        protected ObjectContext(EntityConnection connection, string defaultContainerName)
            : this(connection)
        {
            DefaultContainerName = defaultContainerName;
            if (!string.IsNullOrEmpty(defaultContainerName))
            {
                _disallowSettingDefaultContainerName = true;
            }
        }

        private ObjectContext(EntityConnection connection, bool isConnectionConstructor)
        {
            Debug.Assert(null != connection, "null connection");
            _connection = connection;

            _connection.StateChange += ConnectionStateChange;

            // Ensure a valid connection
            string connectionString = connection.ConnectionString;
            if (connectionString == null || connectionString.Trim().Length == 0)
            {
                throw EntityUtil.InvalidConnection(isConnectionConstructor, null);
            }

            try
            {
                _workspace = RetrieveMetadataWorkspaceFromConnection();
            }
            catch (InvalidOperationException e)
            {
                // Intercept exceptions retrieving workspace, and wrap exception in appropriate
                // message based on which constructor pattern is being used.
                throw EntityUtil.InvalidConnection(isConnectionConstructor, e);
            }

            // Register the O and OC metadata
            if (null != _workspace)
            {
                // register the O-Loader
                if (!_workspace.IsItemCollectionAlreadyRegistered(DataSpace.OSpace))
                {
                    ObjectItemCollection itemCollection = new ObjectItemCollection();
                    _workspace.RegisterItemCollection(itemCollection);
                }

                // have the OC-Loader registered by asking for it
                _workspace.GetItemCollection(DataSpace.OCSpace);
            }

            // load config file properties
            string value = ConfigurationManager.AppSettings[s_UseLegacyPreserveChangesBehavior];
            bool useV35Behavior = false;
            if (Boolean.TryParse(value, out useV35Behavior))
            {
                ContextOptions.UseLegacyPreserveChangesBehavior = useV35Behavior;
            }
        }

        #endregion //Constructors

        #region Properties
        /// <summary>
        /// Gets the connection to the store.
        /// </summary>
        /// <exception cref="ObjectDisposedException">If the <see cref="ObjectContext"/> instance has been disposed.</exception>
        public DbConnection Connection
        {
            get
            {
                if (_connection == null)
                {
                    throw EntityUtil.ObjectContextDisposed();
                }

                return _connection;
            }
        }

        /// <summary>
        /// Gets or sets the default container name.
        /// </summary>
        public string DefaultContainerName
        {
            get
            {
                EntityContainer container = Perspective.GetDefaultContainer();
                return ((null != container) ? container.Name : String.Empty);
            }
            set
            {
                if (!_disallowSettingDefaultContainerName)
                {
                    Perspective.SetDefaultContainer(value);
                }
                else
                {
                    throw EntityUtil.CannotSetDefaultContainerName();
                }
            }
        }

        /// <summary>
        /// Gets the metadata workspace associated with this ObjectContext.
        /// </summary>
        [CLSCompliant(false)]
        public MetadataWorkspace MetadataWorkspace
        {
            get
            {
                return _workspace;
            }
        }

        /// <summary>
        /// Gets the ObjectStateManager used by this ObjectContext.
        /// </summary>
        public ObjectStateManager ObjectStateManager
        {
            get
            {
                if (_cache == null)
                {
                    _cache = new ObjectStateManager(_workspace);
                }
                return _cache;
            }
        }

        /// <summary>
        /// ClrPerspective based on the MetadataWorkspace.
        /// </summary>
        internal ClrPerspective Perspective
        {
            get
            {
                if (_perspective == null)
                {
                    _perspective = new ClrPerspective(_workspace);
                }
                return _perspective;
            }
        }

        /// <summary>
        /// Gets and sets the timeout value used for queries with this ObjectContext.
        /// A null value indicates that the default value of the underlying provider
        /// will be used.
        /// </summary>
        public int? CommandTimeout
        {
            get
            {
                return _queryTimeout;
            }
            set
            {
                if (value.HasValue && value < 0)
                {
                    throw EntityUtil.InvalidCommandTimeout("value");
                }
                _queryTimeout = value;
            }
        }

        /// <summary>
        /// Gets the LINQ query provider associated with this object context.
        /// </summary>
        internal protected IQueryProvider QueryProvider
        {
            get
            {
                if (null == _queryProvider)
                {
                    _queryProvider = new ObjectQueryProvider(this);
                }
                return _queryProvider;
            }
        }

        /// <summary>
        /// Whether or not we are in the middle of materialization
        /// Used to suppress operations such as lazy loading that are not allowed during materialization
        /// </summary>
        internal bool InMaterialization { get; set; }

        /// <summary>
        /// Get <see cref="ObjectContextOptions"/> instance that contains options 
        /// that affect the behavior of the ObjectContext.
        /// </summary>
        /// <value>
        /// Instance of <see cref="ObjectContextOptions"/> for the current ObjectContext.
        /// This value will never be null.
        /// </value>
        public ObjectContextOptions ContextOptions
        {
            get { return _options; }
        }

        #endregion //Properties

        #region Events
        /// <summary>
        /// Property for adding a delegate to the SavingChanges Event.
        /// </summary>
        public event EventHandler SavingChanges
        {
            add { _onSavingChanges += value; }
            remove { _onSavingChanges -= value; }
        }
        /// <summary>
        /// A private helper function for the _savingChanges/SavingChanges event.
        /// </summary>
        private void OnSavingChanges()
        {
            if (null != _onSavingChanges)
            {
                _onSavingChanges(this, new EventArgs());
            }
        }

        /// <summary>
        /// Event raised when a new entity object is materialized.  That is, the event is raised when
        /// a new entity object is created from data in the store as part of a query or load operation.
        /// </summary>
        /// <remarks>
        /// Note that the event is raised after included (spanned) referenced objects are loaded, but
        /// before included (spanned) collections are loaded.  Also, for independent associations,
        /// any stub entities for related objects that have not been loaded will also be created before
        /// the event is raised.
        /// 
        /// It is possible for an entity object to be created and then thrown away if it is determined
        /// that an entity with the same ID already exists in the Context.  This event is not raised
        /// in those cases.
        /// </remarks>
        public event ObjectMaterializedEventHandler ObjectMaterialized
        {
            add { _onObjectMaterialized += value; }
            remove { _onObjectMaterialized -= value; }
        }

        internal void OnObjectMaterialized(object entity)
        {
            if (null != _onObjectMaterialized)
            {
                _onObjectMaterialized(this, new ObjectMaterializedEventArgs(entity));
            }
        }

        /// <summary>
        /// Returns true if any handlers for the ObjectMaterialized event exist.  This is
        /// used for perf reasons to avoid collecting the information needed for the event
        /// if there is no point in firing it.
        /// </summary>
        internal bool OnMaterializedHasHandlers
        {
            get { return _onObjectMaterialized != null && _onObjectMaterialized.GetInvocationList().Length != 0; }
        }

        #endregion //Events

        #region Methods
        /// <summary>
        /// AcceptChanges on all associated entries in the ObjectStateManager so their resultant state is either unchanged or detached.
        /// </summary>
        /// <returns></returns>
        public void AcceptAllChanges()
        {
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();

            if (ObjectStateManager.SomeEntryWithConceptualNullExists())
            {
                throw new InvalidOperationException(Strings.ObjectContext_CommitWithConceptualNull);
            }

            // There are scenarios in which order of calling AcceptChanges does matter:
            // in case there is an entity in Deleted state and another entity in Added state with the same ID -
            // it is necessary to call AcceptChanges on Deleted entity before calling AcceptChanges on Added entity
            // (doing this in the other order there is conflict of keys).
            foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
            {
                entry.AcceptChanges();
            }

            foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
            {
                entry.AcceptChanges();
            }

            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
        }

        private void VerifyRootForAdd(bool doAttach, string entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, out EntitySet entitySet, out bool isNoOperation)
        {
            isNoOperation = false;

            EntitySet entitySetFromName = null;

            if (doAttach)
            {
                // For AttachTo the entity set name is optional
                if (!String.IsNullOrEmpty(entitySetName))
                {
                    entitySetFromName = this.GetEntitySetFromName(entitySetName);
                }
            }
            else
            {
                // For AddObject the entity set name is obligatory
                entitySetFromName = this.GetEntitySetFromName(entitySetName);
            }

            // Find entity set using entity key
            EntitySet entitySetFromKey = null;

            EntityKey key = existingEntry != null ? existingEntry.EntityKey : wrappedEntity.GetEntityKeyFromEntity();
            if (null != (object)key)
            {
                entitySetFromKey = key.GetEntitySet(this.MetadataWorkspace);

                if (entitySetFromName != null)
                {
                    // both entity sets are not null, compare them
                    EntityUtil.ValidateEntitySetInKey(key, entitySetFromName, "entitySetName");
                }
                key.ValidateEntityKey(_workspace, entitySetFromKey);
            }

            entitySet = entitySetFromKey ?? entitySetFromName;

            // Check if entity set was found
            if (entitySet == null)
            {
                throw EntityUtil.EntitySetNameOrEntityKeyRequired();
            }

            this.ValidateEntitySet(entitySet, wrappedEntity.IdentityType);

            // If in the middle of Attach, try to find the entry by key
            if (doAttach && existingEntry == null)
            {
                // If we don't already have a key, create one now
                if (null == (object)key)
                {
                    key = this.ObjectStateManager.CreateEntityKey(entitySet, wrappedEntity.Entity);
                }
                existingEntry = this.ObjectStateManager.FindEntityEntry(key);
            }

            if (null != existingEntry && !(doAttach && existingEntry.IsKeyEntry))
            {
                if (!Object.ReferenceEquals(existingEntry.Entity, wrappedEntity.Entity))
                {
                    throw EntityUtil.ObjectStateManagerContainsThisEntityKey();
                }
                else
                {
                    EntityState exptectedState = doAttach ? EntityState.Unchanged : EntityState.Added;

                    if (existingEntry.State != exptectedState)
                    {
                        throw doAttach ?
                            EntityUtil.EntityAlreadyExistsInObjectStateManager() :
                            EntityUtil.ObjectStateManagerDoesnotAllowToReAddUnchangedOrModifiedOrDeletedEntity(existingEntry.State);
                    }
                    else
                    {
                        // AttachTo:
                        // Attach is no-op when the existing entry is not a KeyEntry
                        // and it's entity is the same entity instance and it's state is Unchanged

                        // AddObject:
                        // AddObject is no-op when the existing entry's entity is the same entity 
                        // instance and it's state is Added
                        isNoOperation = true;
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// Adds an object to the cache.  If it doesn't already have an entity key, the
        /// entity set is determined based on the type and the O-C map.
        /// If the object supports relationships (i.e. it implements IEntityWithRelationships),
        /// this also sets the context onto its RelationshipManager object.
        /// </summary>
        /// <param name="entitySetName">entitySetName the Object to be added. It might be qualifed with container name </param>
        /// <param name="entity">Object to be added.</param>
        public void AddObject(string entitySetName, object entity)
        {
            Debug.Assert(!(entity is IEntityWrapper), "Object is an IEntityWrapper instance instead of the raw entity.");
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            EntityUtil.CheckArgumentNull(entity, "entity");

            EntityEntry existingEntry;
            IEntityWrapper wrappedEntity = EntityWrapperFactory.WrapEntityUsingContextGettingEntry(entity, this, out existingEntry);

            if (existingEntry == null)
            {
                // If the exact object being added is already in the context, there there is no way we need to
                // load the type for it, and since this is expensive, we only do the load if we have to.

                // SQLBUDT 480919: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
                // If the schema types are not loaded: metadata, cache & query would be unable to reason about the type.
                // We will auto-load the entity type's assembly into the ObjectItemCollection.
                // We don't need the user's calling assembly for LoadAssemblyForType since entityType is sufficient.
                MetadataWorkspace.ImplicitLoadAssemblyForType(wrappedEntity.IdentityType, null);
            }
            else
            {
                Debug.Assert((object)existingEntry.Entity == (object)entity, "FindEntityEntry should return null if existing entry contains a different object.");
            }

            EntitySet entitySet;
            bool isNoOperation;

            this.VerifyRootForAdd(false, entitySetName, wrappedEntity, existingEntry, out entitySet, out isNoOperation);
            if (isNoOperation)
            {
                return;
            }

            System.Data.Objects.Internal.TransactionManager transManager = ObjectStateManager.TransactionManager;
            transManager.BeginAddTracking();

            try
            {
                RelationshipManager relationshipManager = wrappedEntity.RelationshipManager;
                Debug.Assert(relationshipManager != null, "Entity wrapper returned a null RelationshipManager");

                bool doCleanup = true;
                try
                {
                    // Add the root of the graph to the cache.
                    AddSingleObject(entitySet, wrappedEntity, "entity");
                    doCleanup = false;
                }
                finally
                {
                    // If we failed after adding the entry but before completely attaching the related ends to the context, we need to do some cleanup.
                    // If the context is null, we didn't even get as far as trying to attach the RelationshipManager, so something failed before the entry
                    // was even added, therefore there is nothing to clean up.
                    if (doCleanup && wrappedEntity.Context == this)
                    {
                        // If the context is not null, it be because the failure happened after it was attached, or it
                        // could mean that this entity was already attached, in which case we don't want to clean it up
                        // If we find the entity in the context and its key is temporary, we must have just added it, so remove it now.
                        EntityEntry entry = this.ObjectStateManager.FindEntityEntry(wrappedEntity.Entity);
                        if (entry != null && entry.EntityKey.IsTemporary)
                        {
                            // devnote: relationshipManager is valid, so entity must be IEntityWithRelationships and casting is safe
                            relationshipManager.NodeVisited = true;
                            // devnote: even though we haven't added the rest of the graph yet, we need to go through the related ends and
                            //          clean them up, because some of them could have been attached to the context before the failure occurred
                            RelationshipManager.RemoveRelatedEntitiesFromObjectStateManager(wrappedEntity);
                            RelatedEnd.RemoveEntityFromObjectStateManager(wrappedEntity);
                        }
                        // else entry was not added or the key is not temporary, so it must have already been in the cache before we tried to add this product, so don't remove anything
                    }
                }
                relationshipManager.AddRelatedEntitiesToObjectStateManager(/*doAttach*/false);
            }
            finally
            {
                transManager.EndAddTracking();
                ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            }
        }
        /// <summary>
        /// Adds an object to the cache without adding its related
        /// entities.
        /// </summary>
        /// <param name="entity">Object to be added.</param>
        /// <param name="setName">EntitySet name for the Object to be added. It may be qualified with container name</param>
        /// <param name="containerName">Container name for the Object to be added.</param>
        /// <param name="argumentName">Name of the argument passed to a public method, for use in exceptions.</param>
        internal void AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, string argumentName)
        {
            Debug.Assert(entitySet != null, "The extent for an entity must be a non-null entity set.");
            Debug.Assert(wrappedEntity != null, "The entity wrapper must not be null.");
            Debug.Assert(wrappedEntity.Entity != null, "The entity must not be null.");

            EntityKey key = wrappedEntity.GetEntityKeyFromEntity();
            if (null != (object)key)
            {
                EntityUtil.ValidateEntitySetInKey(key, entitySet);
                key.ValidateEntityKey(_workspace, entitySet);
            }

            VerifyContextForAddOrAttach(wrappedEntity);
            wrappedEntity.Context = this;
            EntityEntry entry = this.ObjectStateManager.AddEntry(wrappedEntity, (EntityKey)null, entitySet, argumentName, true);

            // If the entity supports relationships, AttachContext on the
            // RelationshipManager object - with load option of
            // AppendOnly (if adding a new object to a context, set
            // the relationships up to cache by default -- load option
            // is only set to other values when AttachContext is
            // called by the materializer). Also add all related entitites to
            // cache.
            //
            // NOTE: AttachContext must be called after adding the object to
            // the cache--otherwise the object might not have a key
            // when the EntityCollections expect it to.            
            Debug.Assert(this.ObjectStateManager.TransactionManager.TrackProcessedEntities, "Expected tracking processed entities to be true when adding.");
            Debug.Assert(this.ObjectStateManager.TransactionManager.ProcessedEntities != null, "Expected non-null collection when flag set.");

            this.ObjectStateManager.TransactionManager.ProcessedEntities.Add(wrappedEntity);

            wrappedEntity.AttachContext(this, entitySet, MergeOption.AppendOnly);

            // Find PK values in referenced principals and use these to set FK values
            entry.FixupFKValuesFromNonAddedReferences();

            _cache.FixupReferencesByForeignKeys(entry);
            wrappedEntity.TakeSnapshotOfRelationships(entry);
        }

        /// <summary>
        /// Explicitly loads a referenced entity or collection of entities into the given entity.
        /// </summary>
        /// <remarks>
        /// After loading, the referenced entity or collection can be accessed through the properties
        /// of the source entity.
        /// </remarks>
        /// <param name="entity">The source entity on which the relationship is defined</param>
        /// <param name="navigationProperty">The name of the property to load</param>
        public void LoadProperty(object entity, string navigationProperty)
        {
            IEntityWrapper wrappedEntity = WrapEntityAndCheckContext(entity, "property");
            wrappedEntity.RelationshipManager.GetRelatedEnd(navigationProperty).Load();
        }

        /// <summary>
        /// Explicitly loads a referenced entity or collection of entities into the given entity.
        /// </summary>
        /// <remarks>
        /// After loading, the referenced entity or collection can be accessed through the properties
        /// of the source entity.
        /// </remarks>
        /// <param name="entity">The source entity on which the relationship is defined</param>
        /// <param name="navigationProperty">The name of the property to load</param>
        /// <param name="mergeOption">The merge option to use for the load</param>
        public void LoadProperty(object entity, string navigationProperty, MergeOption mergeOption)
        {
            IEntityWrapper wrappedEntity = WrapEntityAndCheckContext(entity, "property");
            wrappedEntity.RelationshipManager.GetRelatedEnd(navigationProperty).Load(mergeOption);
        }

        /// <summary>
        /// Explicitly loads a referenced entity or collection of entities into the given entity.
        /// </summary>
        /// <remarks>
        /// After loading, the referenced entity or collection can be accessed through the properties
        /// of the source entity.
        /// The property to load is specified by a LINQ expression which must be in the form of
        /// a simple property member access.  For example, <code>(entity) => entity.PropertyName</code>
        /// where PropertyName is the navigation property to be loaded.  Other expression forms will
        /// be rejected at runtime.
        /// </remarks>
        /// <param name="entity">The source entity on which the relationship is defined</param>
        /// <param name="selector">A LINQ expression specifying the property to load</param>
        public void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector)
        {
            // We used to throw an ArgumentException if the expression contained a Convert.  Now we remove the convert,
            // but if we still need to throw, then we should still throw an ArgumentException to avoid a breaking change.
            // Therefore, we keep track of whether or not we removed the convert.
            bool removedConvert;
            var navProp = ParsePropertySelectorExpression<TEntity>(selector, out removedConvert);
            IEntityWrapper wrappedEntity = WrapEntityAndCheckContext(entity, "property");
            wrappedEntity.RelationshipManager.GetRelatedEnd(navProp, throwArgumentException: removedConvert).Load();
        }

        /// <summary>
        /// Explicitly loads a referenced entity or collection of entities into the given entity.
        /// </summary>
        /// <remarks>
        /// After loading, the referenced entity or collection can be accessed through the properties
        /// of the source entity.
        /// The property to load is specified by a LINQ expression which must be in the form of
        /// a simple property member access.  For example, <code>(entity) => entity.PropertyName</code>
        /// where PropertyName is the navigation property to be loaded.  Other expression forms will
        /// be rejected at runtime.
        /// </remarks>
        /// <param name="entity">The source entity on which the relationship is defined</param>
        /// <param name="selector">A LINQ expression specifying the property to load</param>
        /// <param name="mergeOption">The merge option to use for the load</param>
        public void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector, MergeOption mergeOption)
        {
            // We used to throw an ArgumentException if the expression contained a Convert.  Now we remove the convert,
            // but if we still need to throw, then we should still throw an ArgumentException to avoid a breaking change.
            // Therefore, we keep track of whether or not we removed the convert.
            bool removedConvert;
            var navProp = ParsePropertySelectorExpression<TEntity>(selector, out removedConvert);
            IEntityWrapper wrappedEntity = WrapEntityAndCheckContext(entity, "property");
            wrappedEntity.RelationshipManager.GetRelatedEnd(navProp, throwArgumentException: removedConvert).Load(mergeOption);
        }

        // Wraps the given entity and checks that it has a non-null context (i.e. that is is not detached).
        private IEntityWrapper WrapEntityAndCheckContext(object entity, string refType)
        {
            IEntityWrapper wrappedEntity = EntityWrapperFactory.WrapEntityUsingContext(entity, this);
            if (wrappedEntity.Context == null)
            {
                throw new InvalidOperationException(System.Data.Entity.Strings.ObjectContext_CannotExplicitlyLoadDetachedRelationships(refType));
            }
            if (wrappedEntity.Context != this)
            {
                throw new InvalidOperationException(System.Data.Entity.Strings.ObjectContext_CannotLoadReferencesUsingDifferentContext(refType));
            }
            return wrappedEntity;
        }

        // Validates that the given property selector may represent a navigation property and returns the nav prop string.
        // The actual check that the navigation property is valid is performed by the
        // RelationshipManager while loading the RelatedEnd.
        internal static string ParsePropertySelectorExpression<TEntity>(Expression<Func<TEntity, object>> selector, out bool removedConvert)
        {
            EntityUtil.CheckArgumentNull(selector, "selector");

            // We used to throw an ArgumentException if the expression contained a Convert.  Now we remove the convert,
            // but if we still need to throw, then we should still throw an ArgumentException to avoid a breaking change.
            // Therefore, we keep track of whether or not we removed the convert.
            removedConvert = false;
            var body = selector.Body;
            while (body.NodeType == ExpressionType.Convert || body.NodeType == ExpressionType.ConvertChecked)
            {
                removedConvert = true;
                body = ((UnaryExpression)body).Operand;
            }

            var bodyAsMember = body as MemberExpression;
            if (bodyAsMember == null ||
                !bodyAsMember.Member.DeclaringType.IsAssignableFrom(typeof(TEntity)) ||
                bodyAsMember.Expression.NodeType != ExpressionType.Parameter)
            {
                throw new ArgumentException(System.Data.Entity.Strings.ObjectContext_SelectorExpressionMustBeMemberAccess);
            }
            return bodyAsMember.Member.Name;
        }

        /// <summary>
        /// Apply modified properties to the original object.
        /// This API is obsolete.  Please use ApplyCurrentValues instead.
        /// </summary>
        /// <param name="entitySetName">name of EntitySet of entity to be updated</param>
        /// <param name="changed">object with modified properties</param>
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        [Obsolete("Use ApplyCurrentValues instead")]
        public void ApplyPropertyChanges(string entitySetName, object changed)
        {
            EntityUtil.CheckStringArgument(entitySetName, "entitySetName");
            EntityUtil.CheckArgumentNull(changed, "changed");

            this.ApplyCurrentValues(entitySetName, changed);
        }

        /// <summary>
        /// Apply modified properties to the original object.
        /// </summary>
        /// <param name="entitySetName">name of EntitySet of entity to be updated</param>
        /// <param name="currentEntity">object with modified properties</param>
        public TEntity ApplyCurrentValues<TEntity>(string entitySetName, TEntity currentEntity) where TEntity : class
        {
            EntityUtil.CheckStringArgument(entitySetName, "entitySetName");
            EntityUtil.CheckArgumentNull(currentEntity, "currentEntity");
            IEntityWrapper wrappedEntity = EntityWrapperFactory.WrapEntityUsingContext(currentEntity, this);

            // SQLBUDT 480919: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // If the schema types are not loaded: metadata, cache & query would be unable to reason about the type.
            // We will auto-load the entity type's assembly into the ObjectItemCollection.
            // We don't need the user's calling assembly for LoadAssemblyForType since entityType is sufficient.
            MetadataWorkspace.ImplicitLoadAssemblyForType(wrappedEntity.IdentityType, null);

            EntitySet entitySet = this.GetEntitySetFromName(entitySetName);

            EntityKey key = wrappedEntity.EntityKey;
            if (null != (object)key)
            {
                EntityUtil.ValidateEntitySetInKey(key, entitySet, "entitySetName");
                key.ValidateEntityKey(_workspace, entitySet);
            }
            else
            {
                key = this.ObjectStateManager.CreateEntityKey(entitySet, currentEntity);
            }

            // Check if entity is already in the cache
            EntityEntry ose = this.ObjectStateManager.FindEntityEntry(key);
            if (ose == null || ose.IsKeyEntry)
            {
                throw EntityUtil.EntityNotTracked();
            }

            ose.ApplyCurrentValuesInternal(wrappedEntity);

            return (TEntity)ose.Entity;
        }

        /// <summary>
        /// Apply original values to the entity.
        /// The entity to update is found based on key values of the <paramref name="originalEntity"/> entity and the given <paramref name="entitySetName"/>.
        /// </summary>
        /// <param name="entitySetName">name of EntitySet of entity to be updated</param>
        /// <param name="originalEntity">object with original values</param>
        /// <returns>updated entity</returns>
        public TEntity ApplyOriginalValues<TEntity>(string entitySetName, TEntity originalEntity) where TEntity : class
        {
            EntityUtil.CheckStringArgument(entitySetName, "entitySetName");
            EntityUtil.CheckArgumentNull(originalEntity, "originalEntity");

            IEntityWrapper wrappedOriginalEntity = EntityWrapperFactory.WrapEntityUsingContext(originalEntity, this);

            // SQLBUDT 480919: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // If the schema types are not loaded: metadata, cache & query would be unable to reason about the type.
            // We will auto-load the entity type's assembly into the ObjectItemCollection.
            // We don't need the user's calling assembly for LoadAssemblyForType since entityType is sufficient.
            MetadataWorkspace.ImplicitLoadAssemblyForType(wrappedOriginalEntity.IdentityType, null);

            EntitySet entitySet = this.GetEntitySetFromName(entitySetName);

            EntityKey key = wrappedOriginalEntity.EntityKey;
            if (null != (object)key)
            {
                EntityUtil.ValidateEntitySetInKey(key, entitySet, "entitySetName");
                key.ValidateEntityKey(_workspace, entitySet);
            }
            else
            {
                key = this.ObjectStateManager.CreateEntityKey(entitySet, originalEntity);
            }

            // Check if the entity is already in the cache
            EntityEntry ose = this.ObjectStateManager.FindEntityEntry(key);
            if (ose == null || ose.IsKeyEntry)
            {
                throw EntityUtil.EntityNotTrackedOrHasTempKey();
            }

            if (ose.State != EntityState.Modified &&
                ose.State != EntityState.Unchanged &&
                ose.State != EntityState.Deleted)
            {
                throw EntityUtil.EntityMustBeUnchangedOrModifiedOrDeleted(ose.State);
            }

            if (ose.WrappedEntity.IdentityType != wrappedOriginalEntity.IdentityType)
            {
                throw EntityUtil.EntitiesHaveDifferentType(ose.Entity.GetType().FullName, originalEntity.GetType().FullName);
            }

            ose.CompareKeyProperties(originalEntity);

            // The ObjectStateEntry.UpdateModifiedFields uses a variation of Shaper.UpdateRecord method 
            // which additionaly marks properties as modified as necessary.
            ose.UpdateOriginalValues(wrappedOriginalEntity.Entity);

            // return the current entity
            return (TEntity)ose.Entity;
        }


        /// <summary>
        /// Attach entity graph into the context in the Unchanged state.
        /// This version takes entity which doesn't have to have a Key.
        /// </summary>
        /// <param name="entitySetName">EntitySet name for the Object to be attached. It may be qualified with container name</param>        
        /// <param name="entity"></param>
        public void AttachTo(string entitySetName, object entity)
        {
            Debug.Assert(!(entity is IEntityWrapper), "Object is an IEntityWrapper instance instead of the raw entity.");
            EntityUtil.CheckArgumentNull(entity, "entity");
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();

            EntityEntry existingEntry;
            IEntityWrapper wrappedEntity = EntityWrapperFactory.WrapEntityUsingContextGettingEntry(entity, this, out existingEntry);

            if (existingEntry == null)
            {
                // If the exact object being added is already in the context, there there is no way we need to
                // load the type for it, and since this is expensive, we only do the load if we have to.

                // SQLBUDT 480919: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
                // If the schema types are not loaded: metadata, cache & query would be unable to reason about the type.
                // We will auto-load the entity type's assembly into the ObjectItemCollection.
                // We don't need the user's calling assembly for LoadAssemblyForType since entityType is sufficient.
                MetadataWorkspace.ImplicitLoadAssemblyForType(wrappedEntity.IdentityType, null);
            }
            else
            {
                Debug.Assert((object)existingEntry.Entity == (object)entity, "FindEntityEntry should return null if existing entry contains a different object.");
            }

            EntitySet entitySet;
            bool isNoOperation;

            this.VerifyRootForAdd(true, entitySetName, wrappedEntity, existingEntry, out entitySet, out isNoOperation);
            if (isNoOperation)
            {
                return;
            }

            System.Data.Objects.Internal.TransactionManager transManager = ObjectStateManager.TransactionManager;
            transManager.BeginAttachTracking();

            try
            {
                this.ObjectStateManager.TransactionManager.OriginalMergeOption = wrappedEntity.MergeOption;
                RelationshipManager relationshipManager = wrappedEntity.RelationshipManager;
                Debug.Assert(relationshipManager != null, "Entity wrapper returned a null RelationshipManager");

                bool doCleanup = true;
                try
                {
                    // Attach the root of entity graph to the cache.
                    AttachSingleObject(wrappedEntity, entitySet, "entity");
                    doCleanup = false;
                }
                finally
                {
                    // SQLBU 555615 Be sure that wrappedEntity.Context == this to not try to detach 
                    // entity from context if it was already attached to some other context.
                    // It's enough to check this only for the root of the graph since we can assume that all entities
                    // in the graph are attached to the same context (or none of them is attached).
                    if (doCleanup && wrappedEntity.Context == this)
                    {
                        // SQLBU 509900 RIConstraints: Entity still exists in cache after Attach fails
                        //
                        // Cleaning up is needed only when root of the graph violates some referential constraint.
                        // Normal cleaning is done in RelationshipManager.AddRelatedEntitiesToObjectStateManager()
                        // (referential constraints properties are checked in AttachSingleObject(), before
                        // AddRelatedEntitiesToObjectStateManager is called, that's why normal cleaning
                        // doesn't work in this case)

                        relationshipManager.NodeVisited = true;
                        // devnote: even though we haven't attached the rest of the graph yet, we need to go through the related ends and
                        //          clean them up, because some of them could have been attached to the context.
                        RelationshipManager.RemoveRelatedEntitiesFromObjectStateManager(wrappedEntity);
                        RelatedEnd.RemoveEntityFromObjectStateManager(wrappedEntity);
                    }
                }
                relationshipManager.AddRelatedEntitiesToObjectStateManager(/*doAttach*/true);
            }
            finally
            {
                transManager.EndAttachTracking();
                ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            }
        }
        /// <summary>
        /// Attach entity graph into the context in the Unchanged state.
        /// This version takes entity which does have to have a non-temporary Key.
        /// </summary>
        /// <param name="entity"></param>        
        public void Attach(IEntityWithKey entity)
        {
            EntityUtil.CheckArgumentNull(entity, "entity");

            if (null == (object)entity.EntityKey)
            {
                throw EntityUtil.CannotAttachEntityWithoutKey();
            }

            this.AttachTo(null, entity);
        }
        /// <summary>
        /// Attaches single object to the cache without adding its related entities.
        /// </summary>
        /// <param name="entity">Entity to be attached.</param>
        /// <param name="entitySet">"Computed" entity set.</param>
        /// <param name="argumentName">Name of the argument passed to a public method, for use in exceptions.</param>
        internal void AttachSingleObject(IEntityWrapper wrappedEntity, EntitySet entitySet, string argumentName)
        {
            Debug.Assert(wrappedEntity != null, "entity wrapper shouldn't be null");
            Debug.Assert(wrappedEntity.Entity != null, "entity shouldn't be null");
            Debug.Assert(entitySet != null, "entitySet shouldn't be null");

            // Try to detect if the entity is invalid as soon as possible
            // (before adding the entity to the ObjectStateManager)
            RelationshipManager relationshipManager = wrappedEntity.RelationshipManager;
            Debug.Assert(relationshipManager != null, "Entity wrapper returned a null RelationshipManager");

            EntityKey key = wrappedEntity.GetEntityKeyFromEntity();
            if (null != (object)key)
            {
                EntityUtil.ValidateEntitySetInKey(key, entitySet);
                key.ValidateEntityKey(_workspace, entitySet);
            }
            else
            {
                key = this.ObjectStateManager.CreateEntityKey(entitySet, wrappedEntity.Entity);
            }

            Debug.Assert(key != null, "GetEntityKey should have returned a non-null key");

            // Temporary keys are not allowed
            if (key.IsTemporary)
            {
                throw EntityUtil.CannotAttachEntityWithTemporaryKey();
            }

            if (wrappedEntity.EntityKey != key)
            {
                wrappedEntity.EntityKey = key;
            }

            // Check if entity already exists in the cache.
            // NOTE: This check could be done earlier, but this way I avoid creating key twice.
            EntityEntry entry = ObjectStateManager.FindEntityEntry(key);

            if (null != entry)
            {
                if (entry.IsKeyEntry)
                {
                    // devnote: SQLBU 555615. This method was extracted from PromoteKeyEntry to have consistent
                    // behavior of AttachTo in case of attaching entity which is already attached to some other context.
                    // We can not detect if entity is attached to another context until we call SetChangeTrackerOntoEntity
                    // which throws exception if the change tracker is already set.  
                    // SetChangeTrackerOntoEntity is now called from PromoteKeyEntryInitialization(). 
                    // Calling PromoteKeyEntryInitialization() before calling relationshipManager.AttachContext prevents
                    // overriding Context property on relationshipManager (and attaching relatedEnds to current context).
                    this.ObjectStateManager.PromoteKeyEntryInitialization(this, entry, wrappedEntity, /*shadowValues*/ null, /*replacingEntry*/ false);

                    Debug.Assert(this.ObjectStateManager.TransactionManager.TrackProcessedEntities, "Expected tracking processed entities to be true when adding.");
                    Debug.Assert(this.ObjectStateManager.TransactionManager.ProcessedEntities != null, "Expected non-null collection when flag set.");

                    this.ObjectStateManager.TransactionManager.ProcessedEntities.Add(wrappedEntity);

                    wrappedEntity.TakeSnapshotOfRelationships(entry);

                    this.ObjectStateManager.PromoteKeyEntry(entry,
                        wrappedEntity,
                        /*shadowValues*/ null,
                        /*replacingEntry*/ false,
                        /*setIsLoaded*/ false,
                        /*keyEntryInitialized*/ true,
                        "Attach");

                    ObjectStateManager.FixupReferencesByForeignKeys(entry);

                    relationshipManager.CheckReferentialConstraintProperties(entry);
                }
                else
                {
                    Debug.Assert(!Object.ReferenceEquals(entry.Entity, wrappedEntity.Entity));
                    throw EntityUtil.ObjectStateManagerContainsThisEntityKey();
                }
            }
            else
            {
                VerifyContextForAddOrAttach(wrappedEntity);
                wrappedEntity.Context = this;
                entry = this.ObjectStateManager.AttachEntry(key, wrappedEntity, entitySet, argumentName);

                Debug.Assert(this.ObjectStateManager.TransactionManager.TrackProcessedEntities, "Expected tracking processed entities to be true when adding.");
                Debug.Assert(this.ObjectStateManager.TransactionManager.ProcessedEntities != null, "Expected non-null collection when flag set.");

                this.ObjectStateManager.TransactionManager.ProcessedEntities.Add(wrappedEntity);

                wrappedEntity.AttachContext(this, entitySet, MergeOption.AppendOnly);

                ObjectStateManager.FixupReferencesByForeignKeys(entry);
                wrappedEntity.TakeSnapshotOfRelationships(entry);

                relationshipManager.CheckReferentialConstraintProperties(entry);
            }
        }

        /// <summary>
        /// When attaching we need to check that the entity is not already attached to a different context
        /// before we wipe away that context.
        /// </summary>
        private void VerifyContextForAddOrAttach(IEntityWrapper wrappedEntity)
        {
            if (wrappedEntity.Context != null &&
                wrappedEntity.Context != this &&
                !wrappedEntity.Context.ObjectStateManager.IsDisposed &&
                wrappedEntity.MergeOption != MergeOption.NoTracking)
            {
                throw EntityUtil.EntityCantHaveMultipleChangeTrackers();
            }
        }

        /// <summary>
        /// Create entity key based on given entity set and values of given entity.
        /// </summary>
        /// <param name="entitySetName">entity set of the entity</param>
        /// <param name="entity">entity</param>
        /// <returns>new instance of entity key</returns>
        public EntityKey CreateEntityKey(string entitySetName, object entity)
        {
            Debug.Assert(!(entity is IEntityWrapper), "Object is an IEntityWrapper instance instead of the raw entity.");
            EntityUtil.CheckStringArgument(entitySetName, "entitySetName");
            EntityUtil.CheckArgumentNull(entity, "entity");

            // SQLBUDT 480919: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // If the schema types are not loaded: metadata, cache & query would be unable to reason about the type.
            // We will auto-load the entity type's assembly into the ObjectItemCollection.
            // We don't need the user's calling assembly for LoadAssemblyForType since entityType is sufficient.
            MetadataWorkspace.ImplicitLoadAssemblyForType(EntityUtil.GetEntityIdentityType(entity.GetType()), null);

            EntitySet entitySet = this.GetEntitySetFromName(entitySetName);

            return this.ObjectStateManager.CreateEntityKey(entitySet, entity);
        }

        internal EntitySet GetEntitySetFromName(string entitySetName)
        {
            string setName;
            string containerName;

            ObjectContext.GetEntitySetName(entitySetName, "entitySetName", this, out setName, out containerName);

            // Find entity set using entitySetName and entityContainerName
            return this.GetEntitySet(setName, containerName);
        }

        private void AddRefreshKey(object entityLike, Dictionary<EntityKey, EntityEntry> entities, Dictionary<EntitySet, List<EntityKey>> currentKeys)
        {
            Debug.Assert(!(entityLike is IEntityWrapper), "Object is an IEntityWrapper instance instead of the raw entity.");
            if (null == entityLike)
            {
                throw EntityUtil.NthElementIsNull(entities.Count);
            }

            IEntityWrapper wrappedEntity = EntityWrapperFactory.WrapEntityUsingContext(entityLike, this);
            EntityKey key = wrappedEntity.EntityKey;
            RefreshCheck(entities, entityLike, key);

            // Retrieve the EntitySet for the EntityKey and add an entry in the dictionary
            // that maps a set to the keys of entities that should be refreshed from that set.
            EntitySet entitySet = key.GetEntitySet(this.MetadataWorkspace);

            List<EntityKey> setKeys = null;
            if (!currentKeys.TryGetValue(entitySet, out setKeys))
            {
                setKeys = new List<EntityKey>();
                currentKeys.Add(entitySet, setKeys);
            }

            setKeys.Add(key);
        }

        /// <summary>
        /// Creates an ObjectSet based on the EntitySet that is defined for TEntity.
        /// Requires that the DefaultContainerName is set for the context and that there is a
        /// single EntitySet for the specified type. Throws exception if more than one type is found.
        /// </summary>
        /// <typeparam name="TEntity">Entity type for the requested ObjectSet</typeparam>
        public ObjectSet<TEntity> CreateObjectSet<TEntity>()
            where TEntity : class
        {
            EntitySet entitySet = GetEntitySetForType(typeof(TEntity), "TEntity");
            return new ObjectSet<TEntity>(entitySet, this);
        }

        /// <summary>
        /// Find the EntitySet in the default EntityContainer for the specified CLR type.
        /// Must be a valid mapped entity type and must be mapped to exactly one EntitySet across all of the EntityContainers in the metadata for this context.
        /// </summary>
        /// <param name="entityCLRType">CLR type to use for EntitySet lookup.</param>
        /// <returns></returns>
        private EntitySet GetEntitySetForType(Type entityCLRType, string exceptionParameterName)
        {
            EntitySet entitySetForType = null;

            EntityContainer defaultContainer = this.Perspective.GetDefaultContainer();
            if (defaultContainer == null)
            {
                // We don't have a default container, so look through all EntityContainers in metadata to see if
                // we can find exactly one EntitySet that matches the specified CLR type.
                System.Collections.ObjectModel.ReadOnlyCollection<EntityContainer> entityContainers = this.MetadataWorkspace.GetItems<EntityContainer>(DataSpace.CSpace);
                foreach (EntityContainer entityContainer in entityContainers)
                {
                    // See if this container has exactly one EntitySet for this type
                    EntitySet entitySetFromContainer = GetEntitySetFromContainer(entityContainer, entityCLRType, exceptionParameterName);

                    if (entitySetFromContainer != null)
                    {
                        // Verify we haven't already found a matching EntitySet in some other container
                        if (entitySetForType != null)
                        {
                            // There is more than one EntitySet for this type across all containers in metadata, so we can't determine which one the user intended
                            throw EntityUtil.MultipleEntitySetsFoundInAllContainers(entityCLRType.FullName, exceptionParameterName);
                        }

                        entitySetForType = entitySetFromContainer;
                    }
                }
            }
            else
            {
                // There is a default container, so restrict the search to EntitySets within it
                entitySetForType = GetEntitySetFromContainer(defaultContainer, entityCLRType, exceptionParameterName);
            }

            // We still may not have found a matching EntitySet for this type
            if (entitySetForType == null)
            {
                throw EntityUtil.NoEntitySetFoundForType(entityCLRType.FullName, exceptionParameterName);
            }

            return entitySetForType;
        }

        private EntitySet GetEntitySetFromContainer(EntityContainer container, Type entityCLRType, string exceptionParameterName)
        {
            // Verify that we have an EdmType mapping for the specified CLR type
            EdmType entityEdmType = GetTypeUsage(entityCLRType).EdmType;

            // Try to find a single EntitySet for the specified type
            EntitySet entitySet = null;
            foreach (EntitySetBase es in container.BaseEntitySets)
            {
                // This is a match if the set is an EntitySet (not an AssociationSet) and the EntitySet
                // is defined for the specified entity type. Must be an exact match, not a base type. 
                if (es.BuiltInTypeKind == BuiltInTypeKind.EntitySet && es.ElementType == entityEdmType)
                {
                    if (entitySet != null)
                    {
                        // There is more than one EntitySet for this type, so we can't determine which one the user intended
                        throw EntityUtil.MultipleEntitySetsFoundInSingleContainer(entityCLRType.FullName, container.Name, exceptionParameterName);
                    }

                    entitySet = (EntitySet)es;
                }
            }

            return entitySet;
        }

        /// <summary>
        /// Creates an ObjectSet based on the specified EntitySet name.
        /// </summary>
        /// <typeparam name="TEntity">Expected type of the EntitySet</typeparam>
        /// <param name="entitySetName">
        /// EntitySet to use for the ObjectSet. Can be fully-qualified or unqualified if the DefaultContainerName is set.
        /// </param>
        public ObjectSet<TEntity> CreateObjectSet<TEntity>(string entitySetName)
            where TEntity : class
        {
            EntitySet entitySet = GetEntitySetForNameAndType(entitySetName, typeof(TEntity), "TEntity");
            return new ObjectSet<TEntity>(entitySet, this);
        }

        /// <summary>
        /// Finds an EntitySet with the specified name and verifies that its type matches the specified type.
        /// </summary>
        /// <param name="entitySetName">
        /// Name of the EntitySet to find. Can be fully-qualified or unqualified if the DefaultContainerName is set
        /// </param>
        /// <param name="entityCLRType">
        /// Expected CLR type of the EntitySet. Must exactly match the type for the EntitySet, base types are not valid.
        /// </param>
        /// <param name="exceptionParameterName">Argument name to use if an exception occurs.</param>
        /// <returns>EntitySet that was found in metadata with the specified parameters</returns>
        private EntitySet GetEntitySetForNameAndType(string entitySetName, Type entityCLRType, string exceptionParameterName)
        {
            // Verify that the specified entitySetName exists in metadata
            EntitySet entitySet = GetEntitySetFromName(entitySetName);

            // Verify that the EntitySet type matches the specified type exactly (a base type is not valid)
            EdmType entityEdmType = GetTypeUsage(entityCLRType).EdmType;
            if (entitySet.ElementType != entityEdmType)
            {
                throw EntityUtil.InvalidEntityTypeForObjectSet(entityCLRType.FullName, entitySet.ElementType.FullName, entitySetName, exceptionParameterName);
            }

            return entitySet;
        }

        #region Connection Management

        /// <summary>
        /// Ensures that the connection is opened for an operation that requires an open connection to the store.
        /// Calls to EnsureConnection MUST be matched with a single call to ReleaseConnection.
        /// </summary>
        /// <exception cref="ObjectDisposedException">If the <see cref="ObjectContext"/> instance has been disposed.</exception>
        internal void EnsureConnection()
        {
            if (_connection == null)
            {
                throw EntityUtil.ObjectContextDisposed();
            }

            if (ConnectionState.Closed == Connection.State)
            {
                Connection.Open();
                _openedConnection = true;
            }

            if (_openedConnection)
            {
                _connectionRequestCount++;
            }

            // Check the connection was opened correctly
            if ((_connection.State == ConnectionState.Closed) || (_connection.State == ConnectionState.Broken))
            {
                string message = System.Data.Entity.Strings.EntityClient_ExecutingOnClosedConnection(
                       _connection.State == ConnectionState.Closed ? System.Data.Entity.Strings.EntityClient_ConnectionStateClosed : System.Data.Entity.Strings.EntityClient_ConnectionStateBroken);
                throw EntityUtil.InvalidOperation(message);
            }

            try
            {
                // Make sure the necessary metadata is registered
                EnsureMetadata();

                #region EnsureContextIsEnlistedInCurrentTransaction

                // The following conditions are no longer valid since Metadata Independence.
                Debug.Assert(ConnectionState.Open == _connection.State, "Connection must be open.");

                // IF YOU MODIFIED THIS TABLE YOU MUST UPDATE TESTS IN SaveChangesTransactionTests SUITE ACCORDINGLY AS SOME CASES REFER TO NUMBERS IN THIS TABLE
                //
                // TABLE OF ACTIONS WE PERFORM HERE:
                //
                //  #  lastTransaction     currentTransaction         ConnectionState   WillClose      Action                                  Behavior when no explicit transaction (started with .ElistTransaction())     Behavior with explicit transaction (started with .ElistTransaction())
                //  1   null                null                       Open              No             no-op;                                  implicit transaction will be created and used                                explicit transaction should be used
                //  2   non-null tx1        non-null tx1               Open              No             no-op;                                  the last transaction will be used                                            N/A - it is not possible to EnlistTransaction if another transaction has already enlisted
                //  3   null                non-null                   Closed            Yes            connection.Open();                      Opening connection will automatically enlist into Transaction.Current        N/A - cannot enlist in transaction on a closed connection
                //  4   null                non-null                   Open              No             connection.Enlist(currentTransaction);  currentTransaction enlisted and used                                         N/A - it is not possible to EnlistTransaction if another transaction has already enlisted
                //  5   non-null            null                       Open              No             no-op;                                  implicit transaction will be created and used                                explicit transaction should be used
                //  6   non-null            null                       Closed            Yes            no-op;                                  implicit transaction will be created and used                                N/A - cannot enlist in transaction on a closed connection
                //  7   non-null tx1        non-null tx2               Open              No             connection.Enlist(currentTransaction);  currentTransaction enlisted and used                                         N/A - it is not possible to EnlistTransaction if another transaction has already enlisted
                //  8   non-null tx1        non-null tx2               Open              Yes            connection.Close(); connection.Open();  Re-opening connection will automatically enlist into Transaction.Current     N/A - only applies to TransactionScope - requires two transactions and CommitableTransaction and TransactionScope cannot be mixed
                //  9   non-null tx1        non-null tx2               Closed            Yes            connection.Open();                      Opening connection will automatcially enlist into Transaction.Current        N/A - cannot enlist in transaction on a closed connection

                Transaction currentTransaction = Transaction.Current;

                bool transactionHasChanged = (null != currentTransaction && !currentTransaction.Equals(_lastTransaction))
                                          || (null != _lastTransaction && !_lastTransaction.Equals(currentTransaction));

                if (transactionHasChanged)
                {
                    if (!_openedConnection)
                    {
                        // We didn't open the connection so, just try to enlist the connection in the current transaction. 
                        // Note that the connection can already be enlisted in a transaction (since the user opened 
                        // it s/he could enlist it manually using EntityConnection.EnlistTransaction() method). If the 
                        // transaction the connection is enlisted in has not completed (e.g. nested transaction) this call 
                        // will fail (throw). Also currentTransaction can be null here which means that the transaction
                        // used in the previous operation has completed. In this case we should not enlist the connection
                        // in "null" transaction as the user might have enlisted in a transaction manually between calls by 
                        // calling EntityConnection.EnlistTransaction() method. Enlisting with null would in this case mean "unenlist" 
                        // and would cause an exception (see above). Had the user not enlisted in a transaction between the calls
                        // enlisting with null would be a no-op - so again no reason to do it. 
                        if (currentTransaction != null)
                        {
                            _connection.EnlistTransaction(currentTransaction);
                        }
                    }
                    else if (_connectionRequestCount > 1)
                    {
                        // We opened the connection. In addition we are here because there are multiple
                        // active requests going on (read: enumerators that has not been disposed yet) 
                        // using the same connection. (If there is only one active request e.g. like SaveChanges
                        // or single enumerator there is no need for any specific transaction handling - either
                        // we use the implicit ambient transaction (Transaction.Current) if one exists or we 
                        // will create our own local transaction. Also if there is only one active request
                        // the user could not enlist it in a transaction using EntityConnection.EnlistTransaction()
                        // because we opened the connection).
                        // If there are multiple active requests the user might have "played" with transactions
                        // after the first transaction. This code tries to deal with this kind of changes.

                        if (null == _lastTransaction)
                        {
                            Debug.Assert(currentTransaction != null, "transaction has changed and the lastTransaction was null");

                            // Two cases here: 
                            // - the previous operation was not run inside a transaction created by the user while this one is - just
                            //   enlist the connection in the transaction
                            // - the previous operation ran withing explicit transaction started with EntityConnection.EnlistTransaction()
                            //   method - try enlisting the connection in the transaction. This may fail however if the transactions 
                            //   are nested as you cannot enlist the connection in the transaction until the previous transaction has
                            //   completed.
                            _connection.EnlistTransaction(currentTransaction);
                        }
                        else
                        {
                            // We'll close and reopen the connection to get the benefit of automatic transaction enlistment.
                            // Remarks: We get here only if there is more than one active query (e.g. nested foreach or two subsequent queries or SaveChanges
                            // inside a for each) and each of these queries are using a different transaction (note that using TransactionScopeOption.Required 
                            // will not create a new transaction if an ambient transaction already exists - the ambient transaction will be used and we will 
                            // not end up in this code path). If we get here we are already in a loss-loss situation - we cannot enlist to the second transaction
                            // as this would cause an exception saying that there is already an active transaction that needs to be committed or rolled back
                            // before we can enlist the connection to a new transaction. The other option (and this is what we do here) is to close and reopen
                            // the connection. This will enlist the newly opened connection to the second transaction but will also close the reader being used
                            // by the first active query. As a result when trying to continue reading results from the first query the user will get an exception
                            // saying that calling "Read" on a closed data reader is not a valid operation.
                            _connection.Close();
                            _connection.Open();
                            _openedConnection = true;
                            _connectionRequestCount++;
                        }
                    }
                }
                else
                {
                    // we don't need to do anything, nothing has changed.
                }

                // If we get here, we have an open connection, either enlisted in the current
                // transaction (if it's non-null) or unenlisted from all transactions (if the
                // current transaction is null)
                _lastTransaction = currentTransaction;

                #endregion
            }
            catch (Exception)
            {
                // when the connection is unable to enlist properly or another error occured, be sure to release this connection
                ReleaseConnection();
                throw;
            }

        }

        /// <summary>
        /// Resets the state of connection management when the connection becomes closed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConnectionStateChange(object sender, StateChangeEventArgs e)
        {
            if (e.CurrentState == ConnectionState.Closed)
            {
                _connectionRequestCount = 0;
                _openedConnection = false;
            }
        }

        /// <summary>
        /// Releases the connection, potentially closing the connection if no active operations
        /// require the connection to be open. There should be a single ReleaseConnection call
        /// for each EnsureConnection call.
        /// </summary>
        /// <exception cref="ObjectDisposedException">If the <see cref="ObjectContext"/> instance has been disposed.</exception>
        internal void ReleaseConnection()
        {
            if (_connection == null)
            {
                throw EntityUtil.ObjectContextDisposed();
            }

            if (_openedConnection)
            {
                Debug.Assert(_connectionRequestCount > 0, "_connectionRequestCount is zero or negative");
                if (_connectionRequestCount > 0)
                {
                    _connectionRequestCount--;
                }

                // When no operation is using the connection and the context had opened the connection
                // the connection can be closed
                if (_connectionRequestCount == 0)
                {
                    Connection.Close();
                    _openedConnection = false;
                }
            }
        }

        internal void EnsureMetadata()
        {
            if (!MetadataWorkspace.IsItemCollectionAlreadyRegistered(DataSpace.SSpace))
            {
                Debug.Assert(!MetadataWorkspace.IsItemCollectionAlreadyRegistered(DataSpace.CSSpace), "ObjectContext has C-S metadata but not S?");

                // Only throw an ObjectDisposedException if an attempt is made to access the underlying connection object.
                if (_connection == null)
                {
                    throw EntityUtil.ObjectContextDisposed();
                }

                MetadataWorkspace connectionWorkspace = _connection.GetMetadataWorkspace();

                Debug.Assert(connectionWorkspace.IsItemCollectionAlreadyRegistered(DataSpace.CSpace) &&
                             connectionWorkspace.IsItemCollectionAlreadyRegistered(DataSpace.SSpace) &&
                             connectionWorkspace.IsItemCollectionAlreadyRegistered(DataSpace.CSSpace),
                            "EntityConnection.GetMetadataWorkspace() did not return an initialized workspace?");

                // Validate that the context's MetadataWorkspace and the underlying connection's MetadataWorkspace
                // have the same CSpace collection. Otherwise, an error will occur when trying to set the SSpace
                // and CSSpace metadata
                ItemCollection connectionCSpaceCollection = connectionWorkspace.GetItemCollection(DataSpace.CSpace);
                ItemCollection contextCSpaceCollection = MetadataWorkspace.GetItemCollection(DataSpace.CSpace);
                if (!object.ReferenceEquals(connectionCSpaceCollection, contextCSpaceCollection))
                {
                    throw EntityUtil.ContextMetadataHasChanged();
                }
                MetadataWorkspace.RegisterItemCollection(connectionWorkspace.GetItemCollection(DataSpace.SSpace));
                MetadataWorkspace.RegisterItemCollection(connectionWorkspace.GetItemCollection(DataSpace.CSSpace));
            }
        }

        #endregion

        /// <summary>
        /// Creates an ObjectQuery<typeparamref name="T"/> over the store, ready to be executed.
        /// </summary>
        /// <typeparam name="T">type of the query result</typeparam>
        /// <param name="queryString">the query string to be executed</param>
        /// <param name="parameters">parameters to pass to the query</param>
        /// <returns>an ObjectQuery instance, ready to be executed</returns>
        public ObjectQuery<T> CreateQuery<T>(string queryString, params ObjectParameter[] parameters)
        {
            EntityUtil.CheckArgumentNull(queryString, "queryString");
            EntityUtil.CheckArgumentNull(parameters, "parameters");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // If the schema types are not loaded: metadata, cache & query would be unable to reason about the type.
            // We either auto-load <T>'s assembly into the ObjectItemCollection or we auto-load the user's calling assembly and its referenced assemblies.
            // If the entities in the user's result spans multiple assemblies, the user must manually call LoadFromAssembly.
            // *GetCallingAssembly returns the assembly of the method that invoked the currently executing method.
            MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(T), System.Reflection.Assembly.GetCallingAssembly());

            // create a ObjectQuery<T> with default settings
            ObjectQuery<T> query = new ObjectQuery<T>(queryString, this, MergeOption.AppendOnly);

            foreach (ObjectParameter parameter in parameters)
            {
                query.Parameters.Add(parameter);
            }

            return query;
        }
        /// <summary>
        /// Creates an EntityConnection from the given connection string.
        /// </summary>
        /// <param name="connectionString">the connection string</param>
        /// <returns>the newly created connection</returns>
        [ResourceExposure(ResourceScope.Machine)] //Exposes the file names as part of ConnectionString which are a Machine resource
        [ResourceConsumption(ResourceScope.Machine)] //For EntityConnection constructor. But the paths are not created in this method.
        private static EntityConnection CreateEntityConnection(string connectionString)
        {
            EntityUtil.CheckStringArgument(connectionString, "connectionString");

            // create the connection
            EntityConnection connection = new EntityConnection(connectionString);

            return connection;
        }
        /// <summary>
        /// Given an entity connection, returns a copy of its MetadataWorkspace. Ensure we get
        /// all of the metadata item collections by priming the entity connection.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="ObjectDisposedException">If the <see cref="ObjectContext"/> instance has been disposed.</exception>
        private MetadataWorkspace RetrieveMetadataWorkspaceFromConnection()
        {
            if (_connection == null)
            {
                throw EntityUtil.ObjectContextDisposed();
            }

            MetadataWorkspace connectionWorkspace = _connection.GetMetadataWorkspace(false /* initializeAllConnections */);
            Debug.Assert(connectionWorkspace != null, "EntityConnection.MetadataWorkspace is null.");

            // Create our own workspace
            MetadataWorkspace workspace = connectionWorkspace.ShallowCopy();

            return workspace;
        }
        /// <summary>
        /// Marks an object for deletion from the cache.
        /// </summary>
        /// <param name="entity">Object to be deleted.</param>
        public void DeleteObject(object entity)
        {
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            // This method and ObjectSet.DeleteObject are expected to have identical behavior except for the extra validation ObjectSet
            // requests by passing a non-null expectedEntitySetName. Any changes to this method are expected to be made in the common
            // internal overload below that ObjectSet also uses, unless there is a specific reason why a behavior is desired when the
            // call comes from ObjectContext only.
            DeleteObject(entity, null /*expectedEntitySetName*/);
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
        }

        /// <summary>
        /// Common DeleteObject method that is used by both ObjectContext.DeleteObject and ObjectSet.DeleteObject.
        /// </summary>
        /// <param name="entity">Object to be deleted.</param>
        /// <param name="expectedEntitySet">
        /// EntitySet that the specified object is expected to be in. Null if the caller doesn't want to validate against a particular EntitySet.
        /// </param>
        internal void DeleteObject(object entity, EntitySet expectedEntitySet)
        {
            Debug.Assert(!(entity is IEntityWrapper), "Object is an IEntityWrapper instance instead of the raw entity.");
            EntityUtil.CheckArgumentNull(entity, "entity");

            EntityEntry cacheEntry = this.ObjectStateManager.FindEntityEntry(entity);
            if (cacheEntry == null || !object.ReferenceEquals(cacheEntry.Entity, entity))
            {
                throw EntityUtil.CannotDeleteEntityNotInObjectStateManager();
            }

            if (expectedEntitySet != null)
            {
                EntitySetBase actualEntitySet = cacheEntry.EntitySet;
                if (actualEntitySet != expectedEntitySet)
                {
                    throw EntityUtil.EntityNotInObjectSet_Delete(actualEntitySet.EntityContainer.Name, actualEntitySet.Name, expectedEntitySet.EntityContainer.Name, expectedEntitySet.Name);
                }
            }

            cacheEntry.Delete();
            // Detaching from the context happens when the object
            // actually detaches from the cache (not just when it is
            // marked for deletion).
        }

        /// <summary>
        /// Detach entity from the cache.
        /// </summary>
        /// <param name="entity">Object to be detached.</param>
        public void Detach(object entity)
        {
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            // This method and ObjectSet.DetachObject are expected to have identical behavior except for the extra validation ObjectSet
            // requests by passing a non-null expectedEntitySetName. Any changes to this method are expected to be made in the common
            // internal overload below that ObjectSet also uses, unless there is a specific reason why a behavior is desired when the
            // call comes from ObjectContext only.
            Detach(entity, null /*expectedEntitySet*/);
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
        }

        /// <summary>
        /// Common Detach method that is used by both ObjectContext.Detach and ObjectSet.Detach.
        /// </summary>
        /// <param name="entity">Object to be detached.</param>
        /// <param name="expectedEntitySet">
        /// EntitySet that the specified object is expected to be in. Null if the caller doesn't want to validate against a particular EntitySet.
        /// </param>        
        internal void Detach(object entity, EntitySet expectedEntitySet)
        {
            Debug.Assert(!(entity is IEntityWrapper), "Object is an IEntityWrapper instance instead of the raw entity.");
            EntityUtil.CheckArgumentNull(entity, "entity");

            EntityEntry cacheEntry = this.ObjectStateManager.FindEntityEntry(entity);
            if (cacheEntry == null ||
                !object.ReferenceEquals(cacheEntry.Entity, entity) ||
                cacheEntry.Entity == null) // this condition includes key entries and relationship entries
            {
                throw EntityUtil.CannotDetachEntityNotInObjectStateManager();
            }

            if (expectedEntitySet != null)
            {
                EntitySetBase actualEntitySet = cacheEntry.EntitySet;
                if (actualEntitySet != expectedEntitySet)
                {
                    throw EntityUtil.EntityNotInObjectSet_Detach(actualEntitySet.EntityContainer.Name, actualEntitySet.Name, expectedEntitySet.EntityContainer.Name, expectedEntitySet.Name);
                }
            }

            cacheEntry.Detach();
        }

        /// <summary>
        /// Disposes this ObjectContext.
        /// </summary>
        public void Dispose()
        {
            // Technically, calling GC.SuppressFinalize is not required because the class does not
            // have a finalizer, but it does no harm, protects against the case where a finalizer is added
            // in the future, and prevents an FxCop warning.
            GC.SuppressFinalize(this);
            Dispose(true);
        }

        /// <summary>
        /// Disposes this ObjectContext.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Release managed resources here.

                if (_connection != null)
                {
                    _connection.StateChange -= ConnectionStateChange;

                    // Dispose the connection the ObjectContext created
                    if (_createdConnection)
                    {
                        _connection.Dispose();
                    }
                }
                _connection = null; // Marks this object as disposed.
                _adapter = null;
                if (_cache != null)
                {
                    _cache.Dispose();
                }
            }
            // Release unmanaged resources here (none for this class).
        }

        #region GetEntitySet

        /// <summary>
        /// Returns the EntitySet with the given name from given container.
        /// </summary>
        /// <param name="entitySetName">name of entity set</param>
        /// <param name="entityContainerName">name of container</param>
        /// <returns>the appropriate EntitySet</returns>
        /// <exception cref="InvalidOperationException">the entity set could not be found for the given name</exception>
        /// <exception cref="InvalidOperationException">the entity container could not be found for the given name</exception>
        internal EntitySet GetEntitySet(string entitySetName, string entityContainerName)
        {
            Debug.Assert(entitySetName != null, "entitySetName should be not null");

            EntityContainer container = null;

            if (String.IsNullOrEmpty(entityContainerName))
            {
                container = this.Perspective.GetDefaultContainer();
                Debug.Assert(container != null, "Problem with metadata - default container not found");
            }
            else
            {
                if (!this.MetadataWorkspace.TryGetEntityContainer(entityContainerName, DataSpace.CSpace, out container))
                {
                    throw EntityUtil.EntityContainterNotFoundForName(entityContainerName);
                }
            }

            EntitySet entitySet = null;

            if (!container.TryGetEntitySetByName(entitySetName, false, out entitySet))
            {
                throw EntityUtil.EntitySetNotFoundForName(TypeHelpers.GetFullName(container.Name, entitySetName));
            }

            return entitySet;
        }

        private static void GetEntitySetName(string qualifiedName, string parameterName, ObjectContext context, out  string entityset, out string container)
        {
            entityset = null;
            container = null;
            EntityUtil.CheckStringArgument(qualifiedName, parameterName);

            string[] result = qualifiedName.Split('.');
            if (result.Length > 2)
            {
                throw EntityUtil.QualfiedEntitySetName(parameterName);
            }
            if (result.Length == 1) // if not '.' at all
            {
                entityset = result[0];
            }
            else
            {
                container = result[0];
                entityset = result[1];
                if (container == null || container.Length == 0) // if it starts with '.'
                {
                    throw EntityUtil.QualfiedEntitySetName(parameterName);
                }
            }
            if (entityset == null || entityset.Length == 0) // if it's not in the form "ES name . containername"
            {
                throw EntityUtil.QualfiedEntitySetName(parameterName);
            }

            if (context != null && String.IsNullOrEmpty(container) && (context.Perspective.GetDefaultContainer() == null))
            {
                throw EntityUtil.ContainerQualifiedEntitySetNameRequired(parameterName);
            }
        }

        /// <summary>
        /// Validate that an EntitySet is compatible with a given entity instance's CLR type.
        /// </summary>
        /// <param name="entitySet">an EntitySet</param>
        /// <param name="entityType">The CLR type of an entity instance</param>
        private void ValidateEntitySet(EntitySet entitySet, Type entityType)
        {
            TypeUsage entityTypeUsage = GetTypeUsage(entityType);
            if (!entitySet.ElementType.IsAssignableFrom(entityTypeUsage.EdmType))
            {
                throw EntityUtil.InvalidEntitySetOnEntity(entitySet.Name, entityType, "entity");
            }
        }

        internal TypeUsage GetTypeUsage(Type entityCLRType)
        {
            // Register the assembly so the type information will be sure to be loaded in metadata
            this.MetadataWorkspace.ImplicitLoadAssemblyForType(entityCLRType, System.Reflection.Assembly.GetCallingAssembly());

            TypeUsage entityTypeUsage = null;
            if (!this.Perspective.TryGetType(entityCLRType, out entityTypeUsage) ||
                !TypeSemantics.IsEntityType(entityTypeUsage))
            {
                throw EntityUtil.InvalidEntityType(entityCLRType);
            }
            Debug.Assert(entityTypeUsage != null, "entityTypeUsage is null");
            return entityTypeUsage;
        }
        #endregion

        /// <summary>
        /// Retrieves an object from the cache if present or from the
        /// store if not.
        /// </summary>
        /// <param name="key">Key of the object to be found.</param>
        /// <returns>Entity object.</returns>
        public object GetObjectByKey(EntityKey key)
        {
            EntityUtil.CheckArgumentNull(key, "key");

            EntitySet entitySet = key.GetEntitySet(this.MetadataWorkspace);
            Debug.Assert(entitySet != null, "Key's EntitySet should not be null in the MetadataWorkspace");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // If the schema types are not loaded: metadata, cache & query would be unable to reason about the type.
            // Either the entity type's assembly is already in the ObjectItemCollection or we auto-load the user's calling assembly and its referenced assemblies.
            // *GetCallingAssembly returns the assembly of the method that invoked the currently executing method.
            MetadataWorkspace.ImplicitLoadFromEntityType(entitySet.ElementType, System.Reflection.Assembly.GetCallingAssembly());

            object entity;
            if (!TryGetObjectByKey(key, out entity))
            {
                throw EntityUtil.ObjectNotFound();
            }
            return entity;
        }

        #region Refresh
        /// <summary>
        /// Refreshing cache data with store data for specific entities.
        /// The order in which entites are refreshed is non-deterministic.
        /// </summary>
        /// <param name="refreshMode">Determines how the entity retrieved from the store is merged with the entity in the cache</param>
        /// <param name="collection">must not be null and all entities must be attached to this context. May be empty.</param>
        /// <exception cref="ArgumentOutOfRangeException">if refreshMode is not valid</exception>
        /// <exception cref="ArgumentNullException">collection is null</exception>
        /// <exception cref="ArgumentException">collection contains null or non entities or entities not attached to this context</exception>
        public void Refresh(RefreshMode refreshMode, IEnumerable collection)
        {
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            try
            {
                EntityUtil.CheckArgumentRefreshMode(refreshMode);
                EntityUtil.CheckArgumentNull(collection, "collection");

                // collection may not contain any entities -- this is valid for this overload
                RefreshEntities(refreshMode, collection);
            }
            finally
            {
                ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            }
        }
        /// <summary>
        /// Refreshing cache data with store data for a specific entity.
        /// </summary>
        /// <param name="refreshMode">Determines how the entity retrieved from the store is merged with the entity in the cache</param>
        /// <param name="entity">The entity to refresh. This must be a non-null entity that is attached to this context</param>
        /// <exception cref="ArgumentOutOfRangeException">if refreshMode is not valid</exception>
        /// <exception cref="ArgumentNullException">entity is null</exception>
        /// <exception cref="ArgumentException">entity is not attached to this context</exception>
        public void Refresh(RefreshMode refreshMode, object entity)
        {
            Debug.Assert(!(entity is IEntityWrapper), "Object is an IEntityWrapper instance instead of the raw entity.");
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            try
            {
                EntityUtil.CheckArgumentRefreshMode(refreshMode);
                EntityUtil.CheckArgumentNull(entity, "entity");

                RefreshEntities(refreshMode, new object[] { entity });
            }
            finally
            {
                ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            }
        }

        /// <summary>
        /// Validates that the given entity/key pair has an ObjectStateEntry
        /// and that entry is not in the added state.
        /// 
        /// The entity is added to the entities dictionary, and checked for duplicates.
        /// </summary>
        /// <param name="entities">on exit, entity is added to this dictionary.</param>
        /// <param name="entity">An object reference that is not "Added," has an ObjectStateEntry and is not in the entities list.</param>
        /// <param name="key"></param>
        private void RefreshCheck(
            Dictionary<EntityKey, EntityEntry> entities,
            object entity, EntityKey key)
        {
            Debug.Assert(!(entity is IEntityWrapper), "Object is an IEntityWrapper instance instead of the raw entity.");
            Debug.Assert(entity != null, "The entity is null.");

            EntityEntry entry = ObjectStateManager.FindEntityEntry(key);
            if (null == entry)
            {
                throw EntityUtil.NthElementNotInObjectStateManager(entities.Count);
            }
            if (EntityState.Added == entry.State)
            {
                throw EntityUtil.NthElementInAddedState(entities.Count);
            }
            Debug.Assert(EntityState.Added != entry.State, "not expecting added");
            Debug.Assert(EntityState.Detached != entry.State, "not expecting detached");

            try
            {
                entities.Add(key, entry); // don't ignore duplicates
            }
            catch (ArgumentException)
            {
                throw EntityUtil.NthElementIsDuplicate(entities.Count);
            }

            Debug.Assert(null != entity, "null entity");
            Debug.Assert(null != (object)key, "null entity.Key");
            Debug.Assert(null != key.EntitySetName, "null entity.Key.EntitySetName");
        }

        private void RefreshEntities(RefreshMode refreshMode, IEnumerable collection)
        {
            // refreshMode and collection should already be validated prior to this call -- collection can be empty in one Refresh overload
            // but not in the other, so we need to do that check before we get to this common method
            Debug.Assert(collection != null, "collection may not contain any entities but should never be null");

            bool openedConnection = false;

            try
            {
                Dictionary<EntityKey, EntityEntry> entities = new Dictionary<EntityKey, EntityEntry>(RefreshEntitiesSize(collection));

                #region 1) Validate and bucket the entities by entity set
                Dictionary<EntitySet, List<EntityKey>> refreshKeys = new Dictionary<EntitySet, List<EntityKey>>();
                foreach (object entity in collection) // anything other than object risks InvalidCastException
                {
                    AddRefreshKey(entity, entities, refreshKeys);
                }

                // The collection is no longer required at this point.
                collection = null;
                #endregion

                #region 2) build and execute the query for each set of entities
                if (refreshKeys.Count > 0)
                {
                    EnsureConnection();
                    openedConnection = true;

                    // All entities from a single set can potentially be refreshed in the same query.
                    // However, the refresh operations are batched in an attempt to avoid the generation
                    // of query trees or provider SQL that exhaust available client or server resources.
                    foreach (EntitySet targetSet in refreshKeys.Keys)
                    {
                        List<EntityKey> setKeys = refreshKeys[targetSet];
                        int refreshedCount = 0;
                        while (refreshedCount < setKeys.Count)
                        {
                            refreshedCount = BatchRefreshEntitiesByKey(refreshMode, entities, targetSet, setKeys, refreshedCount);
                        }
                    }
                }

                // The refreshKeys list is no longer required at this point.
                refreshKeys = null;
                #endregion

                #region 3) process the unrefreshed entities
                if (RefreshMode.StoreWins == refreshMode)
                {
                    // remove all entites that have been removed from the store, not added by client
                    foreach (KeyValuePair<EntityKey, EntityEntry> item in entities)
                    {
                        Debug.Assert(EntityState.Added != item.Value.State, "should not be possible");
                        if (EntityState.Detached != item.Value.State)
                        {
                            // We set the detaching flag here even though we are deleting because we are doing a
                            // Delete/AcceptChanges cycle to simulate a Detach, but we can't use Detach directly
                            // because legacy behavior around cascade deletes should be preserved.  However, we
                            // do want to prevent FK values in dependents from being nulled, which is why we
                            // need to set the detaching flag.
                            ObjectStateManager.TransactionManager.BeginDetaching();
                            try
                            {
                                item.Value.Delete();
                            }
                            finally
                            {
                                ObjectStateManager.TransactionManager.EndDetaching();
                            }
                            Debug.Assert(EntityState.Detached != item.Value.State, "not expecting detached");

                            item.Value.AcceptChanges();
                        }
                    }
                }
                else if ((RefreshMode.ClientWins == refreshMode) && (0 < entities.Count))
                {
                    // throw an exception with all appropriate entity keys in text
                    string prefix = String.Empty;
                    StringBuilder builder = new StringBuilder();
                    foreach (KeyValuePair<EntityKey, EntityEntry> item in entities)
                    {
                        Debug.Assert(EntityState.Added != item.Value.State, "should not be possible");
                        if (item.Value.State == EntityState.Deleted)
                        {
                            // Detach the deleted items because this is the client changes and the server
                            // does not have these items any more
                            item.Value.AcceptChanges();
                        }
                        else
                        {
                            builder.Append(prefix).Append(Environment.NewLine);
                            builder.Append('\'').Append(item.Key.ConcatKeyValue()).Append('\'');
                            prefix = ",";
                        }
                    }
                    // If there were items that could not be found, throw an exception
                    if (builder.Length > 0)
                    {
                        throw EntityUtil.ClientEntityRemovedFromStore(builder.ToString());
                    }
                }
                #endregion
            }
            finally
            {
                if (openedConnection)
                {
                    ReleaseConnection();
                }
            }
        }

        private int BatchRefreshEntitiesByKey(RefreshMode refreshMode, Dictionary<EntityKey, EntityEntry> trackedEntities, EntitySet targetSet, List<EntityKey> targetKeys, int startFrom)
        {
            //
            // A single refresh query can be built for all entities from the same set.
            // For each entity set, a DbFilterExpression is constructed that
            // expresses the equivalent of:
            //
            // SELECT VALUE e
            // FROM <entityset> AS e
            // WHERE
            // GetRefKey(GetEntityRef(e)) == <ref1>.KeyValues
            // [OR GetRefKey(GetEntityRef(e)) == <ref2>.KeyValues
            // [..OR GetRefKey(GetEntityRef(e)) == <refN>.KeyValues]]
            //
            // Note that a LambdaFunctionExpression is used so that instead
            // of repeating GetRefKey(GetEntityRef(e)) a VariableReferenceExpression
            // to a Lambda argument with the value GetRefKey(GetEntityRef(e)) is used instead.
            // The query is therefore logically equivalent to:
            //
            // SELECT VALUE e
            // FROM <entityset> AS e
            // WHERE
            //   LET(x = GetRefKey(GetEntityRef(e)) IN (
            //      x == <ref1>.KeyValues
            //     [OR x == <ref2>.KeyValues
            //     [..OR x == <refN>.KeyValues]]
            //   )
            //

            // The batch size determines the maximum depth of the predicate OR tree and
            // also limits the size of the generated provider SQL that is sent to the server.
            const int maxBatch = 250;

            // Bind the target EntitySet under the name "EntitySet".
            DbExpressionBinding entitySetBinding = targetSet.Scan().BindAs("EntitySet");

            // Use the variable from the set binding as the 'e' in a new GetRefKey(GetEntityRef(e)) expression.
            DbExpression sourceEntityKey = entitySetBinding.Variable.GetEntityRef().GetRefKey();

            // Build the where predicate as described above. A maximum of <batchsize> entity keys will be included
            // in the predicate, starting from position <startFrom> in the list of entity keys. As each key is
            // included, both <batchsize> and <startFrom> are incremented to ensure that the batch size is
            // correctly constrained and that the new starting position for the next call to this method is calculated.
            int batchSize = Math.Min(maxBatch, (targetKeys.Count - startFrom));
            DbExpression[] keyFilters = new DbExpression[batchSize];
            for (int idx = 0; idx < batchSize; idx++)
            {
                // Create a row constructor expression based on the key values of the EntityKey.
                KeyValuePair<string, DbExpression>[] keyValueColumns = targetKeys[startFrom++].GetKeyValueExpressions(targetSet);
                DbExpression keyFilter = DbExpressionBuilder.NewRow(keyValueColumns);

                // Create an equality comparison between the row constructor and the lambda variable
                // that refers to GetRefKey(GetEntityRef(e)), which also produces a row
                // containing key values, but for the current entity from the entity set.
                keyFilters[idx] = sourceEntityKey.Equal(keyFilter);
            }

            // Sanity check that the batch includes at least one element.
            Debug.Assert(batchSize > 0, "Didn't create a refresh expression?");

            // Build a balanced binary tree that OR's the key filters together.
            DbExpression entitySetFilter = Helpers.BuildBalancedTreeInPlace(keyFilters, DbExpressionBuilder.Or);

            // Create a FilterExpression based on the EntitySet binding and the Lambda predicate.
            // This FilterExpression encapsulated the logic required for the refresh query as described above.
            DbExpression refreshQuery = entitySetBinding.Filter(entitySetFilter);

            // Initialize the command tree used to issue the refresh query.
            DbQueryCommandTree tree = DbQueryCommandTree.FromValidExpression(this.MetadataWorkspace, DataSpace.CSpace, refreshQuery);

            // Evaluate the refresh query using ObjectQuery<T> and process the results to update the ObjectStateManager.
            MergeOption mergeOption = (RefreshMode.StoreWins == refreshMode ?
                                       MergeOption.OverwriteChanges :
                                       MergeOption.PreserveChanges);

            // The connection will be released by ObjectResult when enumeration is complete.
            this.EnsureConnection();

            try
            {
                ObjectResult<object> results = ObjectQueryExecutionPlan.ExecuteCommandTree<object>(this, tree, mergeOption);

                foreach (object entity in results)
                {
                    // There is a risk that, during an event, the Entity removed itself from the cache.
                    EntityEntry entry = ObjectStateManager.FindEntityEntry(entity);
                    if ((null != entry) && (EntityState.Modified == entry.State))
                    {   // this is 'ForceChanges' - which is the same as PreserveChanges, except all properties are marked modified.
                        Debug.Assert(RefreshMode.ClientWins == refreshMode, "StoreWins always becomes unchanged");
                        entry.SetModifiedAll();
                    }

                    IEntityWrapper wrappedEntity = EntityWrapperFactory.WrapEntityUsingContext(entity, this);
                    EntityKey key = wrappedEntity.EntityKey;
                    EntityUtil.CheckEntityKeyNull(key);

                    // Dev10#673631 - An incorrectly returned entity should result in an exception to avoid further corruption to the OSM.
                    if (!trackedEntities.Remove(key))
                    {
                        throw EntityUtil.StoreEntityNotPresentInClient();
                    }
                }
            }
            catch
            {
                // Enumeration did not complete, so the connection must be explicitly released.
                this.ReleaseConnection();
                throw;
            }

            // Return the position in the list from which the next refresh operation should start.
            // This will be equal to the list count if all remaining entities in the list were
            // refreshed during this call.
            return startFrom;
        }

        private static int RefreshEntitiesSize(IEnumerable collection)
        {
            ICollection list = collection as ICollection;
            return ((null != list) ? list.Count : 0);
        }
        #endregion

        #region SaveChanges
        /// <summary>
        /// Persists all updates to the store.
        /// </summary>
        /// <returns>
        ///   the number of dirty (i.e., Added, Modified, or Deleted) ObjectStateEntries
        ///   in the ObjectStateManager when SaveChanges was called.
        /// </returns>
        public Int32 SaveChanges()
        {
            return SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave);
        }


        /// <summary>
        ///   Persists all updates to the store.
        ///   This API is obsolete.  Please use SaveChanges(SaveOptions options) instead.
        ///   SaveChanges(true) is equivalent to SaveChanges() -- That is it detects changes and
        ///      accepts all changes after save.
        ///   SaveChanges(false) detects changes but does not accept changes after save.
        /// </summary>
        /// <param name="acceptChangesDuringSave">if false, user must call AcceptAllChanges</param>/>
        /// <returns>
        ///   the number of dirty (i.e., Added, Modified, or Deleted) ObjectStateEntries
        ///   in the ObjectStateManager when SaveChanges was called.
        /// </returns>
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        [Obsolete("Use SaveChanges(SaveOptions options) instead.")]
        public Int32 SaveChanges(bool acceptChangesDuringSave)
        {
            return this.SaveChanges(acceptChangesDuringSave ? SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave
                                                            : SaveOptions.DetectChangesBeforeSave);
        }

        /// <summary>
        /// Persists all updates to the store.
        /// </summary>
        /// <param name="options">describes behavior options of SaveChanges</param>
        /// <returns>
        ///   the number of dirty (i.e., Added, Modified, or Deleted) ObjectStateEntries
        ///   in the ObjectStateManager processed by SaveChanges.
        /// </returns>
        public virtual Int32 SaveChanges(SaveOptions options)
        {
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();

            OnSavingChanges();

            if ((SaveOptions.DetectChangesBeforeSave & options) != 0)
            {
                this.ObjectStateManager.DetectChanges();
            }

            if (ObjectStateManager.SomeEntryWithConceptualNullExists())
            {
                throw new InvalidOperationException(Strings.ObjectContext_CommitWithConceptualNull);
            }

            bool mustReleaseConnection = false;
            Int32 entriesAffected = ObjectStateManager.GetObjectStateEntriesCount(EntityState.Added | EntityState.Deleted | EntityState.Modified);
            EntityConnection connection = (EntityConnection)Connection;

            if (0 < entriesAffected)
            {   // else fast exit if no changes to save to avoids interacting with or starting of new transactions

                // get data adapter
                if (_adapter == null)
                {
                    IServiceProvider sp = DbProviderFactories.GetFactory(connection) as IServiceProvider;
                    if (sp != null)
                    {
                        _adapter = sp.GetService(typeof(IEntityAdapter)) as IEntityAdapter;
                    }
                    if (_adapter == null)
                    {
                        throw EntityUtil.InvalidDataAdapter();
                    }
                }
                // only accept changes after the local transaction commits
                _adapter.AcceptChangesDuringUpdate = false;
                _adapter.Connection = connection;
                _adapter.CommandTimeout = this.CommandTimeout;

                try
                {
                    EnsureConnection();
                    mustReleaseConnection = true;

                    // determine what transaction to enlist in
                    bool needLocalTransaction = false;

                    if (null == connection.CurrentTransaction && !connection.EnlistedInUserTransaction)
                    {
                        // If there isn't a local transaction started by the user, we'll attempt to enlist 
                        // on the current SysTx transaction so we don't need to construct a local
                        // transaction.

                        needLocalTransaction = (null == _lastTransaction);
                    }
                    // else the user already has his own local transaction going; user will do the abort or commit.

                    DbTransaction localTransaction = null;
                    try
                    {
                        // EntityConnection tracks the CurrentTransaction we don't need to pass it around
                        if (needLocalTransaction)
                        {
                            localTransaction = connection.BeginTransaction();
                        }
                        entriesAffected = _adapter.Update(ObjectStateManager);

                        if (null != localTransaction)
                        {   // we started the local transaction; so we also commit it
                            localTransaction.Commit();
                        }
                        // else on success with no exception is thrown, user generally commits the transaction
                    }
                    finally
                    {
                        if (null != localTransaction)
                        {   // we started the local transaction; so it requires disposal (rollback if not previously committed
                            localTransaction.Dispose();
                        }
                        // else on failure with an exception being thrown, user generally aborts (default action with transaction without an explict commit)
                    }
                }
                finally
                {
                    if (mustReleaseConnection)
                    {
                        // Release the connection when we are done with the save
                        ReleaseConnection();
                    }
                }

                if ((SaveOptions.AcceptAllChangesAfterSave & options) != 0)
                {   // only accept changes after the local transaction commits

                    try
                    {
                        AcceptAllChanges();
                    }
                    catch (Exception e)
                    {
                        // If AcceptAllChanges throw - let's inform user that changes in database were committed 
                        // and that Context and Database can be in inconsistent state.

                        // We should not be wrapping all exceptions
                        if (EntityUtil.IsCatchableExceptionType(e))
                        {
                            throw EntityUtil.AcceptAllChangesFailure(e);
                        }
                        throw;
                    }
                }
            }
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            return entriesAffected;
        }
        #endregion

        /// <summary>
        /// For every tracked entity which doesn't implement IEntityWithChangeTracker detect changes in the entity's property values
        /// and marks appropriate ObjectStateEntry as Modified.
        /// For every tracked entity which doesn't implement IEntityWithRelationships detect changes in its relationships.
        /// 
        /// The method is used inter----ly by ObjectContext.SaveChanges() but can be also used if user wants to detect changes 
        /// and have ObjectStateEntries in appropriate state before the SaveChanges() method is called.
        /// </summary>
        public void DetectChanges()
        {
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
            this.ObjectStateManager.DetectChanges();
            ObjectStateManager.AssertAllForeignKeyIndexEntriesAreValid();
        }

        /// <summary>
        /// Attempts to retrieve an object from the cache or the store.
        /// </summary>
        /// <param name="key">Key of the object to be found.</param>
        /// <param name="value">Out param for the object.</param>
        /// <returns>True if the object was found, false otherwise.</returns>
        public bool TryGetObjectByKey(EntityKey key, out object value)
        {
            // try the cache first
            EntityEntry entry;
            ObjectStateManager.TryGetEntityEntry(key, out entry); // this will check key argument
            if (entry != null)
            {
                // can't find keys
                if (!entry.IsKeyEntry)
                {
                    // SQLBUDT 511296 returning deleted object.
                    value = entry.Entity;
                    return value != null;
                }
            }

            if (key.IsTemporary)
            {
                // If the key is temporary, we cannot find a corresponding object in the store.
                value = null;
                return false;
            }

            EntitySet entitySet = key.GetEntitySet(this.MetadataWorkspace);
            Debug.Assert(entitySet != null, "Key's EntitySet should not be null in the MetadataWorkspace");

            // Validate the EntityKey values against the EntitySet
            key.ValidateEntityKey(_workspace, entitySet, true /*isArgumentException*/, "key");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            // If the schema types are not loaded: metadata, cache & query would be unable to reason about the type.
            // Either the entity type's assembly is already in the ObjectItemCollection or we auto-load the user's calling assembly and its referenced assemblies.
            // *GetCallingAssembly returns the assembly of the method that invoked the currently executing method.
            MetadataWorkspace.ImplicitLoadFromEntityType(entitySet.ElementType, System.Reflection.Assembly.GetCallingAssembly());

            // Execute the query:
            // SELECT VALUE X FROM [EC].[ES] AS X
            // WHERE X.KeyProp0 = @p0 AND X.KeyProp1 = @p1 AND ... 
            // parameters are the key values 

            // Build the Entity SQL query
            StringBuilder esql = new StringBuilder();
            esql.AppendFormat("SELECT VALUE X FROM {0}.{1} AS X WHERE ", EntityUtil.QuoteIdentifier(entitySet.EntityContainer.Name), EntityUtil.QuoteIdentifier(entitySet.Name));
            EntityKeyMember[] members = key.EntityKeyValues;
            ReadOnlyMetadataCollection<EdmMember> keyMembers = entitySet.ElementType.KeyMembers;
            ObjectParameter[] parameters = new ObjectParameter[members.Length];

            for (int i = 0; i < members.Length; i++)
            {
                if (i > 0)
                {
                    esql.Append(" AND ");
                }
                string parameterName = string.Format(CultureInfo.InvariantCulture, "p{0}", i.ToString(CultureInfo.InvariantCulture));
                esql.AppendFormat("X.{0} = @{1}", EntityUtil.QuoteIdentifier(members[i].Key), parameterName);
                parameters[i] = new ObjectParameter(parameterName, members[i].Value);

                // Try to set the TypeUsage on the ObjectParameter
                EdmMember keyMember = null;
                if (keyMembers.TryGetValue(members[i].Key, true, out keyMember))
                {
                    parameters[i].TypeUsage = keyMember.TypeUsage;
                }
            }

            // Execute the query
            object entity = null;
            ObjectResult<object> results = CreateQuery<object>(esql.ToString(), parameters).Execute(MergeOption.AppendOnly);
            foreach (object queriedEntity in results)
            {
                Debug.Assert(entity == null, "Query for a key returned more than one entity!");
                entity = queriedEntity;
            }

            value = entity;
            return value != null;
        }


        /// <summary>
        /// Executes the given function on the default container. 
        /// </summary>
        /// <typeparam name="TElement">Element type for function results.</typeparam>
        /// <param name="functionName">Name of function. May include container (e.g. ContainerName.FunctionName)
        /// or just function name when DefaultContainerName is known.</param>
        /// <param name="parameters"></param>
        /// <exception cref="ArgumentException">If function is null or empty</exception>
        /// <exception cref="InvalidOperationException">If function is invalid (syntax,
        /// does not exist, refers to a function with return type incompatible with T)</exception>
        public ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, params ObjectParameter[] parameters)
        {
            return ExecuteFunction<TElement>(functionName, MergeOption.AppendOnly, parameters);
        }

        /// <summary>
        /// Executes the given function on the default container. 
        /// </summary>
        /// <typeparam name="TElement">Element type for function results.</typeparam>
        /// <param name="functionName">Name of function. May include container (e.g. ContainerName.FunctionName)
        /// or just function name when DefaultContainerName is known.</param>
        /// <param name="mergeOption"></param>
        /// <param name="parameters"></param>
        /// <exception cref="ArgumentException">If function is null or empty</exception>
        /// <exception cref="InvalidOperationException">If function is invalid (syntax,
        /// does not exist, refers to a function with return type incompatible with T)</exception>
        public ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, MergeOption mergeOption, params ObjectParameter[] parameters)
        {
            EntityUtil.CheckStringArgument(functionName, "function");
            EntityUtil.CheckArgumentNull(parameters, "parameters");

            EdmFunction functionImport;
            EntityCommand entityCommand = CreateEntityCommandForFunctionImport(functionName, out functionImport, parameters);
            int returnTypeCount = Math.Max(1, functionImport.ReturnParameters.Count);
            EdmType[] expectedEdmTypes = new EdmType[returnTypeCount];
            expectedEdmTypes[0] = MetadataHelper.GetAndCheckFunctionImportReturnType<TElement>(functionImport, 0, this.MetadataWorkspace);
            for (int i = 1; i < returnTypeCount; i++)
            {
                if (!MetadataHelper.TryGetFunctionImportReturnType<EdmType>(functionImport, i, out expectedEdmTypes[i]))
                {
                    throw EntityUtil.ExecuteFunctionCalledWithNonReaderFunction(functionImport);
                }
            }

            return CreateFunctionObjectResult<TElement>(entityCommand, functionImport.EntitySets, expectedEdmTypes, mergeOption);
        }

        /// <summary>
        /// Executes the given function on the default container and discard any results returned from the function.
        /// </summary>
        /// <param name="functionName">Name of function. May include container (e.g. ContainerName.FunctionName)
        /// or just function name when DefaultContainerName is known.</param>
        /// <param name="parameters"></param>
        /// <returns>Number of rows affected</returns>
        /// <exception cref="ArgumentException">If function is null or empty</exception>
        /// <exception cref="InvalidOperationException">If function is invalid (syntax,
        /// does not exist, refers to a function with return type incompatible with T)</exception>
        public int ExecuteFunction(string functionName, params ObjectParameter[] parameters)
        {
            EntityUtil.CheckStringArgument(functionName, "function");
            EntityUtil.CheckArgumentNull(parameters, "parameters");

            EdmFunction functionImport;
            EntityCommand entityCommand = CreateEntityCommandForFunctionImport(functionName, out functionImport, parameters);

            EnsureConnection();

            // Prepare the command before calling ExecuteNonQuery, so that exceptions thrown during preparation are not wrapped in CommandCompilationException
            entityCommand.Prepare();

            try
            {
                return entityCommand.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                if (EntityUtil.IsCatchableEntityExceptionType(e))
                {
                    throw EntityUtil.CommandExecution(System.Data.Entity.Strings.EntityClient_CommandExecutionFailed, e);
                }
                throw;
            }
            finally
            {
                this.ReleaseConnection();
            }
        }

        private EntityCommand CreateEntityCommandForFunctionImport(string functionName, out EdmFunction functionImport, params ObjectParameter[] parameters)
        {
            for (int i = 0; i < parameters.Length; i++)
            {
                ObjectParameter parameter = parameters[i];
                if (null == parameter)
                {
                    throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.ObjectContext_ExecuteFunctionCalledWithNullParameter(i));
                }
            }

            string containerName;
            string functionImportName;

            functionImport =
                MetadataHelper.GetFunctionImport(
                functionName, this.DefaultContainerName, this.MetadataWorkspace,
                out containerName, out functionImportName);


            EntityConnection connection = (EntityConnection)this.Connection;

            // create query
            EntityCommand entityCommand = new EntityCommand();
            entityCommand.CommandType = CommandType.StoredProcedure;
            entityCommand.CommandText = containerName + "." + functionImportName;
            entityCommand.Connection = connection;
            if (this.CommandTimeout.HasValue)
            {
                entityCommand.CommandTimeout = this.CommandTimeout.Value;
            }

            PopulateFunctionImportEntityCommandParameters(parameters, functionImport, entityCommand);

            return entityCommand;
        }

        private ObjectResult<TElement> CreateFunctionObjectResult<TElement>(EntityCommand entityCommand, ReadOnlyMetadataCollection<EntitySet> entitySets, EdmType[] edmTypes, MergeOption mergeOption)
        {
            Debug.Assert(edmTypes != null && edmTypes.Length > 0);
            EnsureConnection();

            EntityCommandDefinition commandDefinition = entityCommand.GetCommandDefinition();

            // get store data reader
            DbDataReader storeReader;
            try
            {
                storeReader = commandDefinition.ExecuteStoreCommands(entityCommand, CommandBehavior.Default);
            }
            catch (Exception e)
            {
                this.ReleaseConnection();
                if (EntityUtil.IsCatchableEntityExceptionType(e))
                {
                    throw EntityUtil.CommandExecution(System.Data.Entity.Strings.EntityClient_CommandExecutionFailed, e);
                }
                throw;
            }

            return MaterializedDataRecord<TElement>(entityCommand, storeReader, 0, entitySets, edmTypes, mergeOption);
        }

        /// <summary>
        ///  Get the materializer for the resultSetIndexth result set of storeReader.
        /// </summary>
        internal ObjectResult<TElement> MaterializedDataRecord<TElement>(EntityCommand entityCommand, DbDataReader storeReader, int resultSetIndex, ReadOnlyMetadataCollection<EntitySet> entitySets, EdmType[] edmTypes, MergeOption mergeOption)
        {
            EntityCommandDefinition commandDefinition = entityCommand.GetCommandDefinition();
            try
            {
                // We want the shaper to close the reader if it is the last result set.
                bool shaperOwnsReader = edmTypes.Length <= resultSetIndex + 1;
                EdmType edmType = edmTypes[resultSetIndex];

                //Note: Defensive check for historic reasons, we expect entitySets.Count > resultSetIndex 
                EntitySet entitySet = entitySets.Count > resultSetIndex ? entitySets[resultSetIndex] : null;

                // create the shaper
                System.Data.Common.QueryCache.QueryCacheManager cacheManager = this.Perspective.MetadataWorkspace.GetQueryCacheManager();
                ShaperFactory<TElement> shaperFactory = Translator.TranslateColumnMap<TElement>(cacheManager, commandDefinition.CreateColumnMap(storeReader, resultSetIndex), this.MetadataWorkspace, null, mergeOption, false);
                Shaper<TElement> shaper = shaperFactory.Create(storeReader, this, this.MetadataWorkspace, mergeOption, shaperOwnsReader);

                NextResultGenerator nextResultGenerator;

                // We need to run notifications when the data reader is closed in order to propagate any out parameters.
                // We do this whenever the last (declared) result set's enumerator is disposed (this calls Finally on the shaper)
                // or when the underlying reader is closed as a result of the ObjectResult itself getting disposed.   
                // We use onReaderDisposeHasRun to ensure that this notification is only called once.   
                // the alternative approach of not making the final ObjectResult's disposal result do cleanup doesn't work in the case where
                // its GetEnumerator is called explicitly, and the resulting enumerator is never disposed.
                bool onReaderDisposeHasRun = false; 
                Action<object, EventArgs> onReaderDispose = (object sender, EventArgs e) =>
                    {
                        if (!onReaderDisposeHasRun)
                        {
                            onReaderDisposeHasRun = true;
                            // consume the store reader
                            CommandHelper.ConsumeReader(storeReader);
                            // trigger event callback
                            entityCommand.NotifyDataReaderClosing();
                        }
                    };

                if (shaperOwnsReader)
                {
                    shaper.OnDone += new EventHandler(onReaderDispose);
                    nextResultGenerator = null;
                }
                else
                {
                    nextResultGenerator = new NextResultGenerator(this, entityCommand, edmTypes, entitySets, mergeOption, resultSetIndex + 1);
                }

                // We want the ObjectResult to close the reader in its Dispose method, even if it is not the last result set.
                // This is to allow users to cancel reading results without the unnecessary iteration thru all the result sets.
                return new ObjectResult<TElement>(shaper, entitySet, TypeUsage.Create(edmTypes[resultSetIndex]), true, nextResultGenerator, onReaderDispose);
            }
            catch
            {
                this.ReleaseConnection();
                storeReader.Dispose();
                throw;
            }
        }


        private void PopulateFunctionImportEntityCommandParameters(ObjectParameter[] parameters, EdmFunction functionImport, EntityCommand command)
        {
            // attach entity parameters
            for (int i = 0; i < parameters.Length; i++)
            {
                ObjectParameter objectParameter = parameters[i];
                EntityParameter entityParameter = new EntityParameter();

                FunctionParameter functionParameter = FindParameterMetadata(functionImport, parameters, i);

                if (null != functionParameter)
                {
                    entityParameter.Direction = MetadataHelper.ParameterModeToParameterDirection(
                       functionParameter.Mode);
                    entityParameter.ParameterName = functionParameter.Name;
                }
                else
                {
                    entityParameter.ParameterName = objectParameter.Name;
                }

                entityParameter.Value = objectParameter.Value ?? DBNull.Value;

                if (DBNull.Value == entityParameter.Value ||
                    entityParameter.Direction != ParameterDirection.Input)
                {
                    TypeUsage typeUsage;
                    if (functionParameter != null)
                    {
                        // give precedence to the statically declared type usage
                        typeUsage = functionParameter.TypeUsage;
                    }
                    else if (null == objectParameter.TypeUsage)
                    {
                        Debug.Assert(objectParameter.MappableType != null, "MappableType must not be null");
                        Debug.Assert(Nullable.GetUnderlyingType(objectParameter.MappableType) == null, "Nullable types not expected here.");

                        // since ObjectParameters do not allow users to especify 'facets', make 
                        // sure that the parameter typeusage is not populated with the provider
                        // dafault facet values.
                        // Try getting the type from the workspace. This may fail however for one of the following reasons:
                        // - the type is not a model type
                        // - the types were not loaded into the workspace yet
                        // If the types were not loaded into the workspace we try loading types from the assembly the type lives in and re-try
                        // loading the type. We don't care if the type still cannot be loaded - in this case the result TypeUsage will be null
                        // which we handle later.
                        if (!this.Perspective.TryGetTypeByName(objectParameter.MappableType.FullName, /*ignoreCase */ false, out typeUsage))
                        {
                            this.MetadataWorkspace.ImplicitLoadAssemblyForType(objectParameter.MappableType, null);
                            this.Perspective.TryGetTypeByName(objectParameter.MappableType.FullName, /*ignoreCase */ false, out typeUsage);
                        }
                    }
                    else
                    {
                        typeUsage = objectParameter.TypeUsage;
                    }

                    // set type information (if the provider cannot determine it from the actual value)
                    EntityCommandDefinition.PopulateParameterFromTypeUsage(entityParameter, typeUsage, entityParameter.Direction != ParameterDirection.Input);
                }

                if (entityParameter.Direction != ParameterDirection.Input)
                {
                    ParameterBinder binder = new ParameterBinder(entityParameter, objectParameter);
                    command.OnDataReaderClosing += new EventHandler(binder.OnDataReaderClosingHandler);
                }
                command.Parameters.Add(entityParameter);
            }
        }

        private static FunctionParameter FindParameterMetadata(EdmFunction functionImport, ObjectParameter[] parameters, int ordinal)
        {
            // Retrieve parameter information from functionImport.
            // We first attempt to resolve by case-sensitive name. If there is no exact match,
            // check if there is a case-insensitive match. Case insensitive matches are only permitted
            // when a single parameter would match.
            FunctionParameter functionParameter;
            string parameterName = parameters[ordinal].Name;
            if (!functionImport.Parameters.TryGetValue(parameterName, false, out functionParameter))
            {
                // if only one parameter has this name, try a case-insensitive lookup
                int matchCount = 0;
                for (int i = 0; i < parameters.Length && matchCount < 2; i++)
                {
                    if (StringComparer.OrdinalIgnoreCase.Equals(parameters[i].Name, parameterName))
                    {
                        matchCount++;
                    }
                }
                if (matchCount == 1)
                {
                    functionImport.Parameters.TryGetValue(parameterName, true, out functionParameter);
                }
            }
            return functionParameter;
        }

        /// <summary>
        /// Attempt to generate a proxy type for each type in the supplied enumeration.
        /// </summary>
        /// <param name="types">
        /// Enumeration of Type objects that should correspond to O-Space types.
        /// </param>
        /// <remarks>
        /// Types in the enumeration that do not map to an O-Space type are ignored.
        /// Also, there is no guarantee that a proxy type will be created for a given type,
        /// only that if a proxy can be generated, then it will be generated.
        /// 
        /// See <see cref="EntityProxyFactory"/> class for more information about proxy type generation.
        /// </remarks>

        // Use one of the following methods to retrieve an enumeration of all CLR types mapped to O-Space EntityType objects:
        // 










        // 

        public void CreateProxyTypes(IEnumerable<Type> types)
        {
            ObjectItemCollection ospaceItems = (ObjectItemCollection)MetadataWorkspace.GetItemCollection(DataSpace.OSpace);

            // Ensure metadata is loaded for each type,
            // and attempt to create proxy type only for types that have a mapping to an O-Space EntityType.
            EntityProxyFactory.TryCreateProxyTypes(
                types.Select(type =>
                {
                    // Ensure the assembly containing the entity's CLR type is loaded into the workspace.
                    MetadataWorkspace.ImplicitLoadAssemblyForType(type, null);

                    EntityType entityType;
                    ospaceItems.TryGetItem<EntityType>(type.FullName, out entityType);
                    return entityType;
                }).Where(entityType => entityType != null)
            );
        }

        /// <summary>
        /// Return an enumerable of the current set of CLR proxy types.
        /// </summary>
        /// <returns>
        /// Enumerable of the current set of CLR proxy types.
        /// This will never be null.
        /// </returns>
        public static IEnumerable<Type> GetKnownProxyTypes()
        {
            return EntityProxyFactory.GetKnownProxyTypes();
        }

        /// <summary>
        /// Given a type that may represent a known proxy type, 
        /// return the corresponding type being proxied.
        /// </summary>
        /// <param name="type">Type that may represent a proxy type.</param>
        /// <returns>
        /// Non-proxy type that corresponds to the supplied proxy type,
        /// or the supplied type if it is not a known proxy type.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If the value of the type parameter is null.
        /// </exception
        public static Type GetObjectType(Type type)
        {
            EntityUtil.CheckArgumentNull(type, "type");

            return EntityProxyFactory.IsProxyType(type) ? type.BaseType : type;
        }

        /// <summary>
        /// Create an appropriate instance of the type <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">
        /// Type of object to be returned.
        /// </typeparam>
        /// <returns>
        /// An instance of an object of type <typeparamref name="T"/>.
        /// The object will either be an instance of the exact type <typeparamref name="T"/>,
        /// or possibly an instance of the proxy type that corresponds to <typeparamref name="T"/>.
        /// </returns>
        /// <remarks>
        /// The type <typeparamref name="T"/> must have an OSpace EntityType representation.
        /// </remarks>
        public T CreateObject<T>()
            where T : class
        {
            T instance = null;
            Type clrType = typeof(T);

            // Ensure the assembly containing the entity's CLR type is loaded into the workspace.
            MetadataWorkspace.ImplicitLoadAssemblyForType(clrType, null);

            // Retrieve the OSpace EntityType that corresponds to the supplied CLR type.
            // This call ensure that this mapping exists.
            ClrEntityType entityType = MetadataWorkspace.GetItem<ClrEntityType>(clrType.FullName, DataSpace.OSpace);
            EntityProxyTypeInfo proxyTypeInfo = null;

            if (ContextOptions.ProxyCreationEnabled && ((proxyTypeInfo = EntityProxyFactory.GetProxyType(entityType)) != null))
            {
                instance = (T)proxyTypeInfo.CreateProxyObject();

                // After creating the proxy we need to add additional state to the proxy such
                // that it is able to function correctly when returned.  In particular, it needs
                // an initialized set of RelatedEnd objects because it will not be possible to
                // create these for convention based mapping once the metadata in the context has
                // been lost.
                IEntityWrapper wrappedEntity = EntityWrapperFactory.CreateNewWrapper(instance, null);
                wrappedEntity.InitializingProxyRelatedEnds = true;
                try
                {
                    // We're setting the context temporarily here so that we can go through the process
                    // of creating RelatedEnds even with convention-based mapping.
                    // However, we also need to tell the wrapper that we're doing this so that we don't
                    // try to do things that we normally do when we have a context, such as adding the
                    // context to the RelatedEnds.  We can't do these things since they require an
                    // EntitySet, and, because of MEST, we don't have one.
                    wrappedEntity.AttachContext(this, null, MergeOption.NoTracking);
                    proxyTypeInfo.SetEntityWrapper(wrappedEntity);
                    if (proxyTypeInfo.InitializeEntityCollections != null)
                    {
                        proxyTypeInfo.InitializeEntityCollections.Invoke(null, new object[] { wrappedEntity });
                    }
                }
                finally
                {
                    wrappedEntity.InitializingProxyRelatedEnds = false;
                    wrappedEntity.DetachContext();
                }
            }
            else
            {
                Func<object> ctor = LightweightCodeGenerator.GetConstructorDelegateForType(entityType) as Func<object>;
                Debug.Assert(ctor != null, "Could not find entity constructor");
                instance = ctor() as T;
            }

            return instance;
        }

        /// <summary>
        /// Execute a command against the database server that does not return a sequence of objects.
        /// The command is specified using the server's native query language, such as SQL.
        /// </summary>
        /// <param name="command">The command specified in the server's native query language.</param>
        /// <param name="parameters">The parameter values to use for the query.</param>
        /// <returns>A single integer return value</returns>
        public int ExecuteStoreCommand(string commandText, params object[] parameters)
        {
            this.EnsureConnection();

            try
            {
                DbCommand command = CreateStoreCommand(commandText, parameters);
                return command.ExecuteNonQuery();
            }
            finally
            {
                this.ReleaseConnection();
            }
        }

        /// <summary>
        /// Execute the sequence returning query against the database server. 
        /// The query is specified using the server's native query language, such as SQL.
        /// </summary>
        /// <typeparam name="TElement">The element type of the result sequence.</typeparam>
        /// <param name="query">The query specified in the server's native query language.</param>
        /// <param name="parameters">The parameter values to use for the query.</param>
        /// <returns>An IEnumerable sequence of objects.</returns>
        [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Microsoft: Generic parameters are required for strong-typing of the return type.")]
        public ObjectResult<TElement> ExecuteStoreQuery<TElement>(string commandText, params object[] parameters)
        {
            return ExecuteStoreQueryInternal<TElement>(commandText, null /*entitySetName*/, MergeOption.AppendOnly, parameters);
        }

        /// <summary>
        /// Execute the sequence returning query against the database server. 
        /// The query is specified using the server's native query language, such as SQL.
        /// </summary>
        /// <typeparam name="TEntity">The element type of the resulting sequence</typeparam>
        /// <param name="reader">The DbDataReader to translate</param>
        /// <param name="entitySetName">The entity set in which results should be tracked. Null indicates there is no entity set.</param>
        /// <param name="mergeOption">Merge option to use for entity results.</param>
        /// <returns>The translated sequence of objects</returns>
        [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Microsoft: Generic parameters are required for strong-typing of the return type.")]
        public ObjectResult<TEntity> ExecuteStoreQuery<TEntity>(string commandText, string entitySetName, MergeOption mergeOption, params object[] parameters)
        {
            EntityUtil.CheckStringArgument(entitySetName, "entitySetName");
            return ExecuteStoreQueryInternal<TEntity>(commandText, entitySetName, mergeOption, parameters);
        }

        /// <summary>
        /// See ExecuteStoreQuery method.
        /// </summary>
        private ObjectResult<TElement> ExecuteStoreQueryInternal<TElement>(string commandText, string entitySetName, MergeOption mergeOption, params object[] parameters)
        {
            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type
            // is loaded into the workspace. If the schema types are not loaded
            // metadata, cache & query would be unable to reason about the type. We
            // either auto-load <TElement>'s assembly into the ObjectItemCollection or we
            // auto-load the user's calling assembly and its referenced assemblies.
            // If the entities in the user's result spans multiple assemblies, the
            // user must manually call LoadFromAssembly. *GetCallingAssembly returns
            // the assembly of the method that invoked the currently executing method.
            this.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TElement), System.Reflection.Assembly.GetCallingAssembly());

            this.EnsureConnection();
            DbDataReader reader = null;

            try
            {
                DbCommand command = CreateStoreCommand(commandText, parameters);
                reader = command.ExecuteReader();
            }
            catch
            {
                // We only release the connection when there is an exception. Otherwise, the ObjectResult is
                // in charge of releasing it.
                this.ReleaseConnection();
                throw;
            }

            try
            {
                return InternalTranslate<TElement>(reader, entitySetName, mergeOption, true);
            }
            catch
            {
                reader.Dispose();
                this.ReleaseConnection();
                throw;
            }
        }

        /// <summary>
        /// Translates the data from a DbDataReader into sequence of objects.
        /// </summary>
        /// <typeparam name="TElement">The element type of the resulting sequence</typeparam>
        /// <param name="reader">The DbDataReader to translate</param>
        /// <param name="mergeOption">Merge option to use for entity results.</param>
        /// <returns>The translated sequence of objects</returns>
        [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Microsoft: Generic parameters are required for strong-typing of the return type.")]
        public ObjectResult<TElement> Translate<TElement>(DbDataReader reader)
        {
            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type
            // is loaded into the workspace. If the schema types are not loaded
            // metadata, cache & query would be unable to reason about the type. We
            // either auto-load <TElement>'s assembly into the ObjectItemCollection or we
            // auto-load the user's calling assembly and its referenced assemblies.
            // If the entities in the user's result spans multiple assemblies, the
            // user must manually call LoadFromAssembly. *GetCallingAssembly returns
            // the assembly of the method that invoked the currently executing method.
            this.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TElement), System.Reflection.Assembly.GetCallingAssembly());

            return InternalTranslate<TElement>(reader, null /*entitySetName*/, MergeOption.AppendOnly, false);
        }

        /// <summary>
        /// Translates the data from a DbDataReader into sequence of entities.
        /// </summary>
        /// <typeparam name="TEntity">The element type of the resulting sequence</typeparam>
        /// <param name="reader">The DbDataReader to translate</param>
        /// <param name="entitySetName">The entity set in which results should be tracked. Null indicates there is no entity set.</param>
        /// <param name="mergeOption">Merge option to use for entity results.</param>
        /// <returns>The translated sequence of objects</returns>
        [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Microsoft: Generic parameters are required for strong-typing of the return type.")]
        public ObjectResult<TEntity> Translate<TEntity>(DbDataReader reader, string entitySetName, MergeOption mergeOption)
        {
            EntityUtil.CheckStringArgument(entitySetName, "entitySetName");

            // SQLBUDT 447285: Ensure the assembly containing the entity's CLR type
            // is loaded into the workspace. If the schema types are not loaded
            // metadata, cache & query would be unable to reason about the type. We
            // either auto-load <TEntity>'s assembly into the ObjectItemCollection or we
            // auto-load the user's calling assembly and its referenced assemblies.
            // If the entities in the user's result spans multiple assemblies, the
            // user must manually call LoadFromAssembly. *GetCallingAssembly returns
            // the assembly of the method that invoked the currently executing method.
            this.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TEntity), System.Reflection.Assembly.GetCallingAssembly());

            return InternalTranslate<TEntity>(reader, entitySetName, mergeOption, false);
        }

        private ObjectResult<TElement> InternalTranslate<TElement>(DbDataReader reader, string entitySetName, MergeOption mergeOption, bool readerOwned)
        {
            EntityUtil.CheckArgumentNull(reader, "reader");
            EntityUtil.CheckArgumentMergeOption(mergeOption);
            EntitySet entitySet = null;
            if (!string.IsNullOrEmpty(entitySetName))
            {
                entitySet = this.GetEntitySetFromName(entitySetName);
            }

            // make sure all metadata is available (normally this is handled by the call to EntityConnection.Open,
            // but translate does not necessarily use the EntityConnection)
            EnsureMetadata();

            // get the expected EDM type
            EdmType modelEdmType;
            Type unwrappedTElement = Nullable.GetUnderlyingType(typeof(TElement)) ?? typeof(TElement);
            CollectionColumnMap columnMap;
            // for enums that are not in the model we use the enum underlying type
            if (MetadataHelper.TryDetermineCSpaceModelType<TElement>(this.MetadataWorkspace, out modelEdmType) ||
                (unwrappedTElement.IsEnum && MetadataHelper.TryDetermineCSpaceModelType(unwrappedTElement.GetEnumUnderlyingType(), this.MetadataWorkspace, out modelEdmType)))
            {
                if (entitySet != null && !entitySet.ElementType.IsAssignableFrom(modelEdmType))
                {
                    throw EntityUtil.InvalidOperation(Strings.ObjectContext_InvalidEntitySetForStoreQuery(entitySet.EntityContainer.Name,
                        entitySet.Name, typeof(TElement)));
                }

                columnMap = ColumnMapFactory.CreateColumnMapFromReaderAndType(reader, modelEdmType, entitySet, null);
            }
            else
            {
                columnMap = ColumnMapFactory.CreateColumnMapFromReaderAndClrType(reader, typeof(TElement), this.MetadataWorkspace);
            }

            // build a shaper for the column map to produce typed results
            System.Data.Common.QueryCache.QueryCacheManager cacheManager = this.MetadataWorkspace.GetQueryCacheManager();
            ShaperFactory<TElement> shaperFactory = Translator.TranslateColumnMap<TElement>(cacheManager, columnMap, this.MetadataWorkspace, null, mergeOption, false);
            Shaper<TElement> shaper = shaperFactory.Create(reader, this, this.MetadataWorkspace, mergeOption, readerOwned);
            return new ObjectResult<TElement>(shaper, entitySet, MetadataHelper.GetElementType(columnMap.Type), readerOwned);
        }

        private DbCommand CreateStoreCommand(string commandText, params object[] parameters)
        {
            DbCommand command = this._connection.StoreConnection.CreateCommand();
            command.CommandText = commandText;

            // get relevant state from the object context
            if (this.CommandTimeout.HasValue)
            {
                command.CommandTimeout = this.CommandTimeout.Value;
            }
            EntityTransaction entityTransaction = this._connection.CurrentTransaction;
            if (null != entityTransaction)
            {
                command.Transaction = entityTransaction.StoreTransaction;
            }

            if (null != parameters && parameters.Length > 0)
            {
                DbParameter[] dbParameters = new DbParameter[parameters.Length];

                // three cases: all explicit DbParameters, no explicit DbParameters
                // or a mix of the two (throw in the last case)
                if (parameters.All(p => p is DbParameter))
                {
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        dbParameters[i] = (DbParameter)parameters[i];
                    }
                }
                else if (!parameters.Any(p => p is DbParameter))
                {
                    string[] parameterNames = new string[parameters.Length];
                    string[] parameterSql = new string[parameters.Length];
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        parameterNames[i] = string.Format(CultureInfo.InvariantCulture, "p{0}", i);
                        dbParameters[i] = command.CreateParameter();
                        dbParameters[i].ParameterName = parameterNames[i];
                        dbParameters[i].Value = parameters[i] ?? DBNull.Value;

                        // By default, we attempt to swap in a SQL Server friendly representation of the parameter.
                        // For other providers, users may write:
                        //
                        //      ExecuteStoreQuery("select * from foo f where f.X = ?", 1);
                        //
                        // rather than:
                        //
                        //      ExecuteStoreQuery("select * from foo f where f.X = {0}", 1);
                        parameterSql[i] = "@" + parameterNames[i];
                    }
                    command.CommandText = string.Format(CultureInfo.InvariantCulture, command.CommandText, parameterSql);
                }
                else
                {
                    throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.ObjectContext_ExecuteCommandWithMixOfDbParameterAndValues);
                }

                command.Parameters.AddRange(dbParameters);
            }

            return command;
        }

        /// <summary>
        /// Creates the database using the current store connection and the metadata in the StoreItemCollection. Most of the actual work
        /// is done by the DbProviderServices implementation for the current store connection.
        /// </summary>
        public void CreateDatabase()
        {
            DbConnection storeConnection = this._connection.StoreConnection;
            DbProviderServices services = DbProviderServices.GetProviderServices(this.GetStoreItemCollection().StoreProviderFactory);
            services.CreateDatabase(storeConnection, this.CommandTimeout, this.GetStoreItemCollection());
        }

        /// <summary>
        /// Deletes the database that is specified as the database in the current store connection. Most of the actual work
        /// is done by the DbProviderServices implementation for the current store connection.
        /// </summary>
        public void DeleteDatabase()
        {
            DbConnection storeConnection = this._connection.StoreConnection;
            DbProviderServices services = DbProviderServices.GetProviderServices(this.GetStoreItemCollection().StoreProviderFactory);
            services.DeleteDatabase(storeConnection, this.CommandTimeout, this.GetStoreItemCollection());
        }

        /// <summary>
        /// Checks if the database that is specified as the database in the current store connection exists on the store. Most of the actual work
        /// is done by the DbProviderServices implementation for the current store connection.
        /// </summary>
        public bool DatabaseExists()
        {
            DbConnection storeConnection = this._connection.StoreConnection;
            DbProviderServices services = DbProviderServices.GetProviderServices(this.GetStoreItemCollection().StoreProviderFactory);
            return services.DatabaseExists(storeConnection, this.CommandTimeout, this.GetStoreItemCollection());
        }

        /// <summary>
        /// Creates the sql script that can be used to create the database for the metadata in the StoreItemCollection. Most of the actual work
        /// is done by the DbProviderServices implementation for the current store connection.
        /// </summary>
        public String CreateDatabaseScript()
        {
            DbProviderServices services = DbProviderServices.GetProviderServices(this.GetStoreItemCollection().StoreProviderFactory);
            string targetProviderManifestToken = this.GetStoreItemCollection().StoreProviderManifestToken;
            return services.CreateDatabaseScript(targetProviderManifestToken, this.GetStoreItemCollection());
        }

        private StoreItemCollection GetStoreItemCollection()
        {
            var entityConnection = (EntityConnection)this.Connection;
            // retrieve the item collection from the entity connection rather than the context since:
            // a) it forces creation of the metadata workspace if it's not already there
            // b) the store item collection isn't guaranteed to exist on the context.MetadataWorkspace
            return (StoreItemCollection)entityConnection.GetMetadataWorkspace().GetItemCollection(DataSpace.SSpace);
        }

        #endregion //Methods

        #region Nested types
        /// <summary>
        /// Supports binding EntityClient parameters to Object Services parameters.
        /// </summary>
        private class ParameterBinder
        {
            private readonly EntityParameter _entityParameter;
            private readonly ObjectParameter _objectParameter;

            internal ParameterBinder(EntityParameter entityParameter, ObjectParameter objectParameter)
            {
                _entityParameter = entityParameter;
                _objectParameter = objectParameter;
            }

            internal void OnDataReaderClosingHandler(object sender, EventArgs args)
            {
                // When the reader is closing, out/inout parameter values are set on the EntityParameter
                // instance. Pass this value through to the corresponding ObjectParameter.
                if (_entityParameter.Value != DBNull.Value && _objectParameter.MappableType.IsEnum)
                {
                    _objectParameter.Value = Enum.ToObject(_objectParameter.MappableType, _entityParameter.Value);
                }
                else
                {
                    _objectParameter.Value = _entityParameter.Value;
                }
            }
        }
        #endregion

        internal CollectionColumnMap ColumnMapBuilder { get; set; }
    }
}