On entity Framework additions and deletions and transaction operations

Source: Internet
Author: User

1. Add Objects

Dbentity db = new dbentity ();//Create an object entity, note that all properties need to be assigned (in addition to the auto-grow primary key), if not assigned, the database will be set to null (note whether it is nullable) var user = new user{ Name = "Bomo", age = +, Gender = "Male"};d B. User.add (user);d B. SaveChanges ();

2. Delete the object, delete the primary key that only needs the object

New// Delete only need primary key, here Delete the primary key is 5 of the row varnew5}; // attaching an entity to the object Manager db. User.attach (user); // method One: db. User.remove (user); // method Two: Change the state of the current entity to delete // db. Entry (user). state = entitystate.deleted; Db. SaveChanges ();

3. Modify the Object

Method One:

Dbentity db =Newdbentity (); //The modification requires assigning a value to the primary key, note that all fields need to be assigned, and fields without assignments are updated with NULL to the database            varuser =NewUser {Id=5, Name="Bomo", Age= +, Gender="male"                }; //attaching an entity to the object Managerdb.            User.attach (user); //change the state of the current entity to modifiedDb. Entry (user). State =entitystate.modified; Db. SaveChanges ();

Method Two: Every time the method one needs to modify all the fields, inefficient, and troublesome, the following describes the modification of some fields

            Dbentity db = new dbentity ();            The modification needs to assign a value to the primary key, note: All fields need to be assigned, the field without assignment is updated with NULL to the database            var user = new user                {                    Id = 5,                    Name = "Bomo", age                    = 21                };            Attaches an entity to the object Manager in            db. User.attach (user);                        Gets the state entity to user and can modify its state            var setentry = ((iobjectcontextadapter) db). ObjectContext.ObjectStateManager.GetObjectStateEntry (user);            Only the Name property of the entity and The Age attribute            setentry.setmodifiedproperty ("name") are modified;            Setentry.setmodifiedproperty ("Age");            Db. SaveChanges ();

4. Use transactions: It is very simple to use transactions, just put the required operations in TransactionScope, and finally commit

            Dbentity db = new dbentity ();            using (var scope = new TransactionScope ())            {                //Perform multiple operations                var user1 = new User                {                    Name = "Bomo", age                    =                    Gender = "male"                };                Db. User.add (user1);                Db. SaveChanges ();                                var user2 = new User                {                    Name = "Toroto", age                    = $,                    Gender = "female"                };                Db. User.add (user2);                Db. SaveChanges ();                Commit TRANSACTION                scope.complete ();            }

5, Query: Query through LINQ query

            Dbentity db = new dbentity ();            Select the partial field            var user = db. User.where (U = u.name = = "Bomo"). Select (U + = new {Id = u.id, Name = u.name, age = U.age}). FirstOrDefault ();            Only functions that call FirstOrDefault, first, single, ToList, ToArray, and so on will execute queries against the database.

Excerpt Source: http://www.cnblogs.com/bomo/p/3331602.html

On entity Framework additions and deletions and transaction 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.