In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to use DataBase First mode to add, delete, modify and query the database", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use DataBase First mode to achieve database additions, deletions, changes and queries" can help you solve your doubts.
I. New data
Add a new Student with the following code:
Static void Add () {using (StudentSystemEntities dbContext = new StudentSystemEntities ()) {/ / define Student object Student stu = new Student () {StudentName = "flower thousand bones", Sex = "female", Age = 3422, Major = "dance major", Email = "2345678911@qq.com"} / / output the SQL statement executed by EF to the console dbContext.Database.Log + = p = > Console.WriteLine (p); / / just the operation dbContext.Students.Add (stu) above memory; / / saved database dbContext.SaveChanges (); Console.WriteLine ("saved successfully");}}
Call the Add () method in the Main () method, and the result:
View the database results:
From the two screenshots above, we can see that the data has been added successfully.
Second, query data
Query the student information that StudentName is a Tang monk and output:
Static void Query () {using (StudentSystemEntities dbContext = new StudentSystemEntities ()) {List list = dbContext.Students.Where (p = > p.StudentName = = "Tang monk"). ToList (); list.ForEach (p = > {Console.WriteLine ("name:" + p.StudentName + ", age:" + p.Age+ ", major:" + p.Major+ ", email:" + p.Email). );}}
Call Query () in the Main () method, and the result:
3. Modify data
Change StudentName to the age of the student with thousands of bones:
Static void Edit () {using (StudentSystemEntities dbContext = new StudentSystemEntities ()) {/ / output the SQL statement executed by EF to the console dbContext.Database.Log + = p = > Console.WriteLine (p); / / query the data to be modified, and FirstOrDefault represents the first piece of data Student student = dbContext.Students.Where (p = > p.StudentName = "Hua Qiangu") .FirstOrDefault () / / Save modification student.Age = 234; dbContext.SaveChanges (); Console.WriteLine ("modified successfully");}}
Results:
View database data:
4. Delete data
Delete the newly added data:
Static void Delete () {using (StudentSystemEntities dbContext = new StudentSystemEntities ()) {/ / output the SQL statement executed by EF to the console dbContext.Database.Log + = p = > Console.WriteLine (p); / / query first and then delete Student stu = new Student () {StudentID=18} / / append to the collection dbContext.Students.Attach (stu); / / the deleted SQL statement is the dbContext.Students.Remove generated from stu's StudentID (stu); / / Save dbContext.SaveChanges (); Console.WriteLine ("deleted successfully");}}
Results:
View the database:
The data was deleted successfully.
After reading this, the article "how to use DataBase First mode to add, delete, modify and query the database" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 288
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.