The previous days learned to use the entity Framework in the Vs2010console project and use code First mode. Generate a table for the database by writing the model class. Also, write data to the table and get all the data in the table. This is far from the actual application, has not been able to make additions and deletions to the database of all operations. This article records how other SQL operations are performed in EF.
In the previous article, I wrote a Saveuser method. This method inserts an instance data from the users class into the database, using the USERDAL.USER.ADD () method. Place the cursor at the Add method, F12 navigate to the class where the method resides, and you can see the inside of the class. It is found that there are many methods in this class (including the previous add). The last line has a SQLQuery method, and the parameters need to happen to be SQL strings and parameters.
Queries in EF, support for LINQ to SQL, and queries for SQL statements. With SQLQuery This method, the operation of the database is no problem, and the traditional way of ADO is no different, just construct the required SQL statement. The following example is a query statement with parameters:
Public voidFinduser () {userdal Userdal=NewUserdal (); Console.WriteLine ("Input UserName want to find"); stringUserName =Console.ReadLine (); List<Users> Users = UserDal.users.SqlQuery ("SELECT * from t_users where UserName like '%"+username+"% '"). ToList (); foreach(Users Iteminchusers) {Console.WriteLine ("UserName: {0}", item. UserName); Console.WriteLine ("userpwd: {0}", item. USERPWD); Console.WriteLine ("useremail: {0}", item. Email); Console.WriteLine ("Usertel: {0}", item. Tel); Console.WriteLine ("*****------**********---------********"); } console.readkey (); }
Similarly, you can write delete and modify operations:
Public voidDeleteUser () {userdal Userdal=NewUserdal (); UserDal.users.SqlQuery ("Delete from t_users where UserId = {0}",2); List<Users> Users = UserDal.users.SqlQuery ("SELECT * from T_users"). ToList (); foreach(Users Iteminchusers) {Console.WriteLine ("UserName: {0}", item. UserName); Console.WriteLine ("userpwd: {0}", item. USERPWD); Console.WriteLine ("useremail: {0}", item. Email); Console.WriteLine ("Usertel: {0}", item. Tel); Console.WriteLine ("*****------**********---------********"); } console.readkey (); } Public voidUpdateusersbyid () {userdal Userdal=NewUserdal (); UserDal.users.SqlQuery ("Update t_users Set UserName = {1} where UserId = {0}",2,"yyyyxxxx"); List<Users> Users = UserDal.users.SqlQuery ("SELECT * from T_users"). ToList (); foreach(Users Iteminchusers) {Console.WriteLine ("UserName: {0}", item. UserName); Console.WriteLine ("userpwd: {0}", item. USERPWD); Console.WriteLine ("useremail: {0}", item. Email); Console.WriteLine ("Usertel: {0}", item. Tel); Console.WriteLine ("*****------**********---------********"); } console.readkey (); }
Next, in a different way. querying data Using LINQ to SQL (Spit slot: Follow Microsoft, there will always be something to learn.) Learn MVC to extend EF and learn EF to extend to LINQ. Although this thing has been out early ~ ~). The following method uses LINQ to SQL to query the table for user information in the username containing the input string. The 8th, 9, and 10 lines are the way LINQ is queried.
1 Public voidDisplayuserbyname ()2 {3 stringUserName =string. Empty;4Console.WriteLine ("Input UserName:");5UserName =console.readline ();6Userdal Userdal =NewUserdal ();7List<users> Users =userDal.users.ToList ();8 varquery = fromBinchUsers9 whereb.username.contains (UserName)Ten Selectb; One foreach(Users Iteminchquery) A { -Console.WriteLine ("UserName: {0}", item. UserName); -Console.WriteLine ("userpwd: {0}", item. USERPWD); theConsole.WriteLine ("useremail: {0}", item. Email); -Console.WriteLine ("Usertel: {0}", item. Tel); - } -}
Here, you can already use EF to perform common operations on the database. But there are so many methods under the Dbset class that we still need to go on. At the same time, EF is not just the Code first mode (which still has problems, when the model changes, the original database is deleted and the new database is re-established.) This is not to be encountered during the development process, as it will remove the large amount of test data from the previous input. EF can also connect to existing data and map existing database tables to model classes. (This is the essence of EF as an ORM framework)
To be Continued ~ ~
Entity Framework Notes (II)