1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Data . Entity ;
4+ using System . Linq ;
5+ using System . Text ;
6+ using RefactorThis . GraphDiff ;
7+
8+ namespace Z . EntityFrameworkGraphDiff . labEF6
9+ {
10+ public class Request_LazyLoad
11+ {
12+ public static void Execute ( )
13+ {
14+ // Create BD
15+ using ( var context = new EntityContext ( ) )
16+ {
17+ My . CreateBD ( context ) ;
18+ }
19+
20+ // CLEAN
21+ using ( var context = new EntityContext ( ) )
22+ {
23+ context . EntitySimples . RemoveRange ( context . EntitySimples ) ;
24+ context . EntitySimpleChilds . RemoveRange ( context . EntitySimpleChilds ) ;
25+ context . EntitySimpleLists . RemoveRange ( context . EntitySimpleLists ) ;
26+
27+ context . SaveChanges ( ) ;
28+ }
29+
30+ var entity = new EntitySimple { ColumnInt = 0 } ;
31+ // SEED
32+ using ( var context = new EntityContext ( ) )
33+ {
34+ entity . EntitySimpleChild = new EntitySimpleChild ( ) { ColumnInt = 1 } ;
35+ var EntitySimpleLists = new List < EntitySimpleList > ( ) { new EntitySimpleList ( ) { ColumnInt = 2 } , new EntitySimpleList ( ) { ColumnInt = 3 } } ;
36+
37+ context . EntitySimples . Add ( entity ) ;
38+
39+ context . EntitySimpleLists . AddRange ( EntitySimpleLists ) ;
40+ context . SaveChanges ( ) ;
41+ }
42+
43+ EntitySimpleList entityChildList = null ;
44+ using ( var context = new EntityContext ( ) )
45+ {
46+ entity = context . EntitySimples . Include ( x => x . EntitySimpleChild ) . First ( ) ;
47+ entityChildList = context . EntitySimpleLists . First ( ) ;
48+ entity . EntitySimpleLists = new List < EntitySimpleList > ( ) { entityChildList } ;
49+ }
50+
51+ entityChildList . ColumnInt = 20 ;
52+ entityChildList . EntitySimpleList2s = new List < EntitySimpleList2 > ( ) { new EntitySimpleList2 ( ) } ;
53+
54+ // TEST
55+ using ( var context = new EntityContext ( ) )
56+ {
57+
58+ context . UpdateGraph ( entity , map => map . AssociatedEntity ( c => entity . EntitySimpleLists ) . AssociatedCollection ( c => c . EntitySimpleLists ) ) ;
59+ //context.UpdateGraph(entity.EntitySimpleLists.First(), map => map.AssociatedEntity(c => c.EntitySimpleList2s));
60+ context . SaveChanges ( ) ;
61+ }
62+
63+ using ( var context = new EntityContext ( ) )
64+ {
65+ var a = context . EntitySimples . ToList ( ) ;
66+ var b = context . EntitySimpleChilds . ToList ( ) ;
67+ var c = context . EntitySimpleLists . ToList ( ) ;
68+ }
69+ }
70+
71+ public class EntityContext : DbContext
72+ {
73+ public EntityContext ( ) : base ( My . ConnectionString )
74+ {
75+ }
76+
77+ public DbSet < EntitySimple > EntitySimples { get ; set ; }
78+ public DbSet < EntitySimpleChild > EntitySimpleChilds { get ; set ; }
79+ public DbSet < EntitySimpleList > EntitySimpleLists { get ; set ; }
80+ public DbSet < EntitySimpleList2 > EntitySimpleList2s { get ; set ; }
81+
82+ protected override void OnModelCreating ( DbModelBuilder modelBuilder )
83+ {
84+ base . OnModelCreating ( modelBuilder ) ;
85+ Configuration . LazyLoadingEnabled = true ;
86+ Configuration . ProxyCreationEnabled = true ;
87+ }
88+ }
89+
90+ public class EntitySimple
91+ {
92+ public int ID { get ; set ; }
93+ public int ColumnInt { get ; set ; }
94+ public String ColumnString { get ; set ; }
95+
96+ public virtual EntitySimpleChild EntitySimpleChild { get ; set ; }
97+ public virtual ICollection < EntitySimpleList > EntitySimpleLists { get ; set ; }
98+ }
99+
100+
101+ public class EntitySimpleChild
102+ {
103+ public int ID { get ; set ; }
104+ public int ColumnInt { get ; set ; }
105+ public String ColumnString { get ; set ; }
106+ public virtual ICollection < EntitySimpleList2 > EntitySimpleList2s { get ; set ; }
107+ }
108+ public class EntitySimpleList
109+ {
110+ public int ID { get ; set ; }
111+ public int ColumnInt { get ; set ; }
112+ public String ColumnString { get ; set ; }
113+ public virtual ICollection < EntitySimpleList2 > EntitySimpleList2s { get ; set ; }
114+ }
115+
116+ public class EntitySimpleList2
117+ {
118+ public int ID { get ; set ; }
119+ public int ColumnInt { get ; set ; }
120+ public String ColumnString { get ; set ; }
121+ }
122+ }
123+ }
0 commit comments