Mvc--mvc+linq+ef checking and pruning of data sheets

Source: Internet
Author: User
Tags wrapper

The composition of MVC and the role of each part are briefly described in the previous blog post. This blog will introduce the MVC+LINQ+EF framework for querying, deleting, and modifying data tables.


first, what is LINQ.

LINQ is a. NET language-integrated query based on relational data that is used to manage relational data in the form of objects. In this blog instance, LINQ is used primarily for querying data.


say again what is EF.

When it comes to EF, I have to say it first. Orm,orm Full name: (object-relation Mapping) object-relational mapping. ORM is the representation of business data in relational databases in the form of objects, and the process of implementing the business logic of the system through object-oriented way to organize these objects. ORM does the mapping between relational data and object data, and ORM can generate SQL statements automatically through mapping relationships, and ORM acts as a bridge between the business logic layer and the data layer.

When we use the application to perform CRUD on the database, through EF, it is actually the operation of ObjectContext, ObjectContext equivalent to the entry of EF, ObjectContext get the corresponding message (CRUD), The object o is mapped to the relationship R in the database using the mapping in the ORM. We look at what is stored in mapping in the diagram.



Querying, deleting, modifying instances of data tables

    

1. Use Dbfirst to map the database.





    

2. Create a new controller in the Controllers folder and write out queries, deletions, and modifications using the EF Framework and LINQ.

        <summary>//
        Query database student name///</summary>//
        <returns></returns>
        Public ActionResult Index ()
        {
            //Use LINQ to query the student name in the data context
            list<models.t_student> list = (from d in DB. T_student Select D). ToList ();

            Pass collection data to view
            viewdata["DataList"] = list;
            return View ();
        

  <span style= "White-space:pre" >	</span>///<summary>//Delete students///
        </summary based on student ID
        >//
        <param name= "id" > Student id</param>//
        <returns></returns> public
        ActionResult Del (string id)
        {
            //creates the entity to be deleted and assigns the ID to the entity object
            t_student Modeldel = new T_student () {studentid = id};

            //Add the Entity object to the EF Management container
            db. T_student. Attach (Modeldel);

            Marks the Entity object wrapper class as delete state
            db. T_student. Remove (Modeldel);

            Update Database
            db. SaveChanges ();

            The update succeeds, jumps to index
            return redirecttoaction ("index", "MyClass");}

     #region display the data to be modified [httpget]///<summary>///Show data to be modified///</summary> <param name= "id" > students to be modified id</param>///<returns></returns> public ActionResult Modify (string id) {//based on the student ID, query the database, return the collection to get the first entity object T_student ts = (from a in db. t_student where A.studentid = = ID Select a).

            FirstOrDefault (); Query Course name ienumerable<selectlistitem> listitem= (from C in DB. T_class select C). ToList ().
            
            Select (C=>new selectlistitem{value=c.classid.tostring (), text=c.classname});

            The name of the course queried to ViewBag viewbag.classlist = ListItem;
        Use view to pass data to a property named model on the View Return view (TS);
        #endregion #region Save the data you want to modify [HttpPost]///<summary>//Save the data to be modified </summary>//<param name= "id" > Student id</param> to be modified///<reTurns></returns> public ActionResult Modify (t_student ts) {//Add the Entity object to the EF object container and get the wrapper class object Dbentityentry<t_student> entry=db.

            entry<t_student> (TS); Set the wrapper class to Unchange entry.

            state = System.Data.EntityState.Unchanged; Sets the property entry to be changed. Property (A=>a.studentname).
            Ismodified=true; Entry. Property (A = A.classid).

            IsModified = true; Commit the update to database db.

            SaveChanges ();
        Update successful, jump to index return redirecttoaction ("index", "MyClass"); } #endregion

    

3. Add Query List view (index.cshtml)

@using mymvctest.models @{Layout = null;} <! DOCTYPE html>  

    

4. Add "Modify" View (modify.cshtml)

@model MyMvcTest.Models.T_student @{Layout = null;} <! DOCTYPE html>  
 


Summarize:

Through the implementation of this example, from the macro understanding of the integration of MVC and EF use of the advantages of the play. It also learns the power of EF, which allows programmers to focus more on business logic than on the writing of SQL statements.


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.