Local Data
The Local property of DBSet provides simple access to the entities is currently being tracked by T He context, and has not been marked as Deleted. Local keeps track of entities whose entity state is added, modified and unchanged. For example:
usingSystem.Data.Entity;classProgram {Static voidMain (string[] args) { using(varCTX =Newschooldbentities ()) {ctx. Students.load (); CTx. Students.add (NewStudent () {studentname ="New Student" }); varSTD1 = ctx. Students.find (1);//find student whose id = 1CTx. Students.remove (STD1);//Remove student whose id = 1 varSTD2 = ctx. Students.find (2);//find student whose id = 1Std2. Studentname ="Modified Name"; //Loop over the students in context ' s local.Console.WriteLine ("In Local:"); foreach(varStudentinchCTX. students.local) {Console.WriteLine ("Found {0}: {1} with state {2}", student. StudentID, student. Studentname, CTX. Entry (student). State); } //Get all students from DB.Console.WriteLine ("\nin DbSet Query:"); foreach(varStudentinchCTX. Students) {Console.WriteLine ("Found {0}: {1} with state {2}", student. StudentID, student. Studentname, CTX. Entry (student). State); } } } }
Output:
In Local :
Found 0: New Student with state Added
Found 2: Modified Name with state Modified
Found 3: Student3 with state UnchangedIn DbSet query:
Found 1: New Student with state Deleted
Found 2: Modified Name with state Modified
Found 3: Student3 with state Unchanged
As can see in the above output, local keeps track of entities whose state is Added, Modified or unchanged where as DbS Et collection contains all the entities whose are Deleted, Modified or unchanged.
Visit MSDN For more information on Local.
Entity Framework Tutorial Basics: Local Data