Entity Framework Tutorial Basics: Local Data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.