ASP EF Framework, Entity Framework (entityframework), Databasefirst

Source: Internet
Author: User

The DATABASEFIRST:EF framework automatically generates data layers and model based on the database.

Project--Add--New item--Data--ado.net Entity Data Model--build from database--New connection--Select server name, database--[Yes, include sensitive data in connection string]--Select datasheet, view in database, Stored procedure--complete--Overwrite web.config


Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;

Using System.Web.UI.WebControls; namespace WebApplication1 {public partial class WebForm1:System.Web.UI.Page {protected void Page_Load (  Object sender, EventArgs e) {}//Insert protected void Button1_Click (object sender,
            EventArgs e) {UserInfo UserInfo = new UserInfo ();
            Userinfo.email = "sss@256.com";
            Userinfo.regtime = DateTime.Now;
            Userinfo.username = "SSS56";
            Userinfo.userpass= "123456";  Effristmodelentities db = new effristmodelentities (); The class in the Model1.Context.cs. (Context class, which is an interaction with the database through this class) db.
            Userinfo.add (UserInfo);//Adds data to the EF and adds markup. Db.    SaveChanges (); The statement is executed before the data is saved to the database.
            Returns the number of rows affected.
        Response.Write (userinfo.id);
}//query protected void button2_click (object sender, EventArgs e) {            Effristmodelentities db = new effristmodelentities (); LINQ var userinfolist = from u in db.  UserInfo where u.id ==343 select U; A LINQ statement. U is a custom variable, where the type is userinfo type. The query is not executed in the database here.

            Returns a list collection. foreach (UserInfo UserInfo in userinfolist)//ef the deferred loading mechanism, where data is used to query the database.
            Do not query when not in use.
            {Response.Write (userinfo.username);
            }//select * from UserInfo where id=343//from UserInfo the execution order of the SQL statements in the database. where id=343//select *}//delete protected void Button3_Click (object sender, Ev
            
            Entargs e) {effristmodelentities db = new effristmodelentities (); /* The first way to delete var userinfolist = from u in db.
            UserInfo where u.id = = 345 Select U; UserInfo UserInfo=userinfolist.firstordefault (); Returns the first element, and if not, returns a default value of NULL if (UserInfo!= null) {//db. Userinfo.remove (UserInfo);
                Add a delete tag, but you must perform a query operation before you can successfully mark it. Db. Entry<userinfo> (UserInfo). state = System.Data.EntityState.Deleted;

                Add a delete tag, which can be marked directly without the need to execute the query. Db.  SaveChanges ();
            Performs a delete operation.
            } else {Response.Write ("The data to be deleted does not exist!!");
            }//The second deletion method.
            UserInfo UserInfo = new UserInfo () {id=344}; Db. Userinfo.remove (UserInfo);
            You must perform a query operation before marking (infrequently). Db. Entry<userinfo> (UserInfo).  state = System.Data.EntityState.Deleted;
            Add a delete tag, which can be marked directly without the need to execute the query. Db. SaveChanges (); If the data you want to delete does not exist, an error occurs.

        It is best to query before deleting. }//Modify protected void Button4_Click (object sender, EventArgs e) {Effristmodelentitie
            s db = new effristmodelentities (); var userinfolist = from u in db.
            UserInfo where u.id = = 343 Select U;
            var userInfo = Userinfolist.firstordefault ();
                if (userInfo!= null) {Userinfo.userpass = "666666"; Db. Entry<userinfo> (UserInfo).
                state = System.Data.EntityState.Modified; Db.
            SaveChanges ();
            } else {Response.Write ("The data to be modified does not exist!!"); }
        }
    }
}



Related Article

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.