Add, delete, modify, and query operations on EF

Source: Internet
Author: User

namespace consoleapplication3
{< br> class Program
{< br> static void main (string [] ARGs)
{< br> model1container M = new model1container ();
Student s = new student ();
S. name = "James";
M. student set. addobject (s); // Add operation
M. savechanges (); // added running
console. writeline ("added successfully");

var slist = from Stu in M. student set select Stu; // query operation
foreach (student SL in slist)
{< br> console. writeline ("output Student name:");
console. writeline (SL. name);
}

console. writeline ("update operation");
Student SC = new student ();
SC. id = 2;
SC. name = "slightly changed";
M. student set. attach (SC); // This sentence is very important to write the object to be updated to the object set
M. objectstatemanager. changeobjectstate (SC, system. data. entitystate. modified); // update the object
M. savechanges (); // run the update operation.

console. writeline ("delete operation");
Student SD = new student ();
SD. id = 3;
M. student set. attach (SD);
M. objectstatemanager. changeobjectstate (SD, system. data. entitystate. deleted); // delete operation
M. savechanges ();
}< BR >}

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.