ASP. NET Mvc-ef Basics

Source: Internet
Author: User

  

EF is further encapsulated on the basis of ADO, and if I do new projects later I might consider using EF instead of ADO.

Actually a long time ago I had contact with EF, but too long useless, some forget, a few days ago, also made a little notes. The records are as follows:

  

  

1. How to create EF No, look at the XML file that opened EF after EF was created.

First right, select Open with XML text

  

After folding, the SSDL content here defines the database fields in SQL Server.
The CSDL content defines the property fields of the class
CS MAPPING defines a mapping between a database field and a class's attribute field

  

2. Take a look at the TT template

  

Before EF has no TT template, now added, if you understand the source code, you can modify the rules of his codes, but the general situation does not have to modify it.

The ModelGuestBook.Context.tt suffix here is the code that generates the object interface.

The modelguestbook.tt suffix here is the code that generates the Model class.

3. Take a look at the database connection string generated by EF

<add name= "ccxdemoentities" connectionstring= "metadata=res://*/modelguestbook.csdl|res://*/ Modelguestbook.ssdl|res://*/modelguestbook.msl;provider=system.data.sqlclient;provider Connection string=& Quot;data source=.; Initial catalog=ccxdemo; User Id=sa; password=123456; Multipleactiveresultsets=true; app=entityframework&quot; "providername=" System.Data.EntityClient "/>

  

Joined the reflection grew a lot longer. Or to clearly see the database is a change, password and user name is changed, IP is where to change.

4. Take a look at the use of EF to find and change the implementation, become more concise

Ccxdemoentities db =Newccxdemoentities (); protected voidPage_Load (Objectsender, EventArgs e) {                    }        //New        protected voidButton1_Click (Objectsender, EventArgs e) {Gbook_message Gbookobj=NewGbook_message (); Gbookobj.username="CCX"; Gbookobj.body="Hello World"; Gbookobj.ip="127.0.0.1"; Gbookobj.createdate= DateTime.Parse ("2014-12-12");            DB.GBOOK_MESSAGE.ADD (Gbookobj); Db.            SaveChanges (); Response.Write ("ok!!"); }        //Modify        protected voidButton2_Click (Objectsender, EventArgs e) {            //find the Object            varGbookobj = db.gBook_Message.Where (g = G.username = ="CCX").            FirstOrDefault (); //What's changedGbookobj.username ="Chunxiao"; //Perform modificationsdb.        SaveChanges (); }        //Delete        protected voidButton3_Click (Objectsender, EventArgs e) {Gbook_message Gbookobj=NewGbook_message (); Gbookobj.id=1; //Add the object inDb.gBook_Message.Attach (gbookobj); //Mark as DeleteDb.gBook_Message.Remove (gbookobj); //Perform the deletedb.        SaveChanges (); }

ASP. NET Mvc-ef Basics

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.