Add, delete, modify, and query EF instances
Create a context object:WordBoradEntities db = new WordBoradEntities ();
Add:// 1.1Create object
User uObj = new User ()
{
UName ="Andy Lau",
ULoginName = "aaa ",
UPwd = "asdfasdfasdfsadf ",
UIsDel = false,
UAddtime = DateTime. Now
};
// 1.2PassEFAdd to database
// 1.2.1ObjectAddData ContextOfUserCollection
Db. Users. Add (uObj );
// 1.2.2Call data contextOfSaveObjectStored database
Db. SaveChanges ();
Query:List <User> list = db. Users. Where (u => u. uName ="Andy Lau"). ToList ();
Connection query:LinqConnection query:From a in db. User join B in db. UsersAddresses on a. Id equals B. udid
EFConnection query:IQueryable <UsersAddress> addrs = db. User. Include ("UsersAddresses"). Where (a => a. Id = 1 );
Another connection query:Var list = db. user. join (db. usersAddresses, c => c. id, g => g. udid, (c, g) => new {Name = c. name, GroupName = g. groupName });
Delete:EFThere are three ways to delete
Note: versions 1 and 2 are all deleted based on the primary key, and Version 3 is queried to the Database Based on any conditions and then deleted based on the query results, in fact, version 3 is deleted based on the primary key in the query results.
Version 1: Delete based on the primary key
//InstantiateUsersObject, and specifyIdValue
Users user = new Users () {Id = 1 };
//SetUserAttach to the context object and obtainEFContainer Management object
Var entry = db. Entry <User> (user );OrVar entry = db. Entry (user );
//Set the object status to delete
Entry. State = EntityState. Deleted;
//Save changes
Db. SaveChanges ();
Console. WriteLine ("Deleted successfully!");
Version 2: delete based on the primary key
//InstantiateUsersObject, and specifyIdValue
Users user = new Users () {Id = 1 };
//SetUserAttach to context object
Db. Users. Attach (user );
//DeleteUserObject
Db. Users. Remove (user );
//Save changes
Db. SaveChanges ();
Console. WriteLine ("Deleted successfully!");
Version 3: query the conditions and then delete them.
Var list = db. Users. Where (u => u. Name ="Zhang San");
If (list! = Null & list. Any ())
{
Foreach (User item in list)
{
Db. User. Remove (item );
}
}
Db. SaveChanges ();
Modify:
1.Officially recommendedModification Method (query first and then modify)
//SetIdIs2OfUsersData Extraction
Var user = db. Users. Where (u => u. Id = 2). FirstOrDefault ();
Console. WriteLine ("Before modification:"+ User. UserName );
//ModifyUserNameAttribute
User. UserName = "222222222222222 ";
//Save changes
Db. SaveChanges ();
Console. WriteLine ("After modification:"+ User. UserName );
2.Self-optimized modification method
// 1.Create an object to be modified
User use = new User () {uId = 8, uName ="Tom~~~ "};
// 2.SetObjectJoinEFContainer,And obtainCurrent objectOfStatus management object
DbEntityEntry <User> entry = db. Entry <User> (user );
// 3.SetThis objectModified
Entry. State = System. Data. EntityState. Unchanged;
// 4.SetThis objectOfUNameAttributeIsModify the status andEntry. StateChangedModifiedStatus
Entry. Property ("uName"). IsModified = true;
// 5.CloseEFObject validity check (if the created field of the data to be modified is not assigned a value, the object validity check is disabled. If all fields are assigned a value, the object validity check is disabled.EFObject validity check)
Db. Configuration. ValidateOnSaveEnabled = false;
// 6.Save to database again-- EfContextYesAccordingObjectStatus, AccordingEntry. State = ModifiedValueGenerateCorrespondingUpdate SQLStatement.
Db. SaveChanges ();
Console. WriteLine ("Modified successfully:");