C # EF Foundation operations

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Reflection;namespaceMCO. ef{ Public classTest {//Instantiation ContextConnectionsql connection =NewConnectionsql (); //Increase         Public intDataadd () {User User=NewUser () {UserName="Lu Liu", Usersex=true            }; Connection.            User.add (user); returnconnection.        SaveChanges (); }        //Delete         Public intDelete () {List<User> User = connection. user.where<user> (U = u.userid >2). Tolist<user>(); foreach(User uinchuser) {connection.            User.remove (U); }            returnconnection.        SaveChanges (); }        //Modify         Public intUpdate () {List<User> User = connection. user.where<user> (U = u.usersex = =true). Tolist<user>(); foreach(User uinchuser) {U.usersex=false; }            returnconnection.        SaveChanges (); }        //Enquiry        Private voiduserlist () {IQueryable userlist= connection. user.where<user> (U = u.userid >1); //reflection gets all the property values and property names of the entity classPropertyinfo[] attribute =typeof(User).            GetProperties (); //calling the Listtojson methodConsole.Write (Listtojson (attribute, userlist)); }        //reflection stitching into JSON        Private stringListtojson (propertyinfo[] attribute, IQueryable dataList) {StringBuilder Strjson=NewStringBuilder ( the); foreach(varDatainchdataList) {Strjson.append ("{");  for(inti =0; I < attribute. Length; i++)                {                    //Get property names and property values (by reflection)Strjson.appendformat ("\ "{0}\": \ "{1}\",", Attribute[i]. Name, Attribute[i].                GetValue (data)); } strjson.remove (Strjson.length-1,1); Strjson.append ("},"); } strjson.remove (Strjson.length-1,1); returnstrjson.tostring (); }    }}

C # EF Foundation operations

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.