Ado. Net Entities Framework instance

Source: Internet
Author: User

1. Create a database MyTestDB and create a table tb_Student under the database. The table field definition is shown in.

2. Create the console application MyTestDB.

3. Create an ADO. NET object data model under the project.

Right-click the project and choose "add"> "new item"> "data"> "ADO. NET"> "input name"> "add ".

Select "generate from database" and click "Next ".

Select database connection and click "Next" (if no database connection exists, create a new connection)

Select a database object, set the model namespace, and click "finish.

View the project file organization structure. There are two more files, App. Config and MyTestDB. edmx.

4. Add test code

Using System; using System. linq; namespace MyTestDB {public class Program {static void Main (string [] args) {AddNewStudent (); // Add a record to the database: wsp 20 1 80 SelectStudent (); // query the record whose classid is 1, and get: wsp 20 1 80 UpdateStudent (); // modify the wsp age to 22 DeleteStudent (); // Delete wsp information }////// Add student records ///Public static void AddNewStudent () {using (MyTestDBEntities myDbEntity = new MyTestDBEntities () {tb_Student tbStudent = new tb_Student (); tbStudent. name = "wsp"; tbStudent. age = 20; tbStudent. classid = 1; tbStudent. score = 80; myDbEntity. addTotb_Student (tbStudent); int count = myDbEntity. saveChanges ();}}////// Delete student records ///Public static void DeleteStudent () {using (MyTestDBEntities myDbEntity = new MyTestDBEntities () {var query = from student in myDbEntity. tb_Student where student. name = "wsp" select student; if (query! = Null) {foreach (var q in query) {myDbEntity. DeleteObject (q);} myDbEntity. SaveChanges ();}}}////// Update student records ///Public static void UpdateStudent () {using (MyTestDBEntities myDbEntity = new MyTestDBEntities () {var query = from student in myDbEntity. tb_Student where student. name = "wsp" select student; if (query! = Null) {foreach (var q in query) {q. age = 22;} myDbEntity. SaveChanges ();}}}////// Query student records ///Public static void SelectStudent () {using (MyTestDBEntities myDbEntity = new MyTestDBEntities () {var query = from student in myDbEntity. tb_Student where student. classid = 1 select student; if (query! = Null) {foreach (var q in query) {Console. writeLine (q. name + "" + q. age + "" + q. classid + "" + q. score );}}}}}}

This instance completes a simple addition, deletion, modification, and query operation, and does not involve the introduction of Ado. Net Entities Framework theoretical knowledge.

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.