ASP EF架構,Entity Framework(EntityFramework),DataBaseFirst

來源:互聯網
上載者:User

DataBaseFirst:EF架構自動根據資料庫產生資料層和model。

項目--添加--建立項--資料--ADO.NET實體資料模型--從資料庫產生--建立串連--選擇伺服器名、資料庫--[是,在連接字串中包括敏感性資料]--選擇資料庫中的資料表、視圖、預存程序--完成--覆蓋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)        {        }                //插入        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();  //Model1.Context.cs中的類。(上下文類,都是通過該類實現和資料庫的互動)            db.UserInfo.Add(userInfo);//將資料添加到EF並且添加了委任標記。            db.SaveChanges();    //執行該語句,資料才會儲存到資料庫。返回受影響的行數。            Response.Write(userInfo.ID);        }        //查詢        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;  //linq語句。u是自訂變數,在這裡的類型是UserInfo類型。在此處並未在資料庫中執行查詢。返回的是一個list集合。            foreach (UserInfo userInfo in userInfoList)//EF 消極式載入機制,資料用到的時候才去資料庫中查詢。不能用的時候不查詢。            {                Response.Write(userInfo.UserName);            }                        // select * from UserInfo where ID=343            // from UserInfo   資料庫中SQL語句的執行順序。            // where ID=343            // select *        }        //刪除        protected void Button3_Click(object sender, EventArgs e)        {            EFFristModelEntities db = new EFFristModelEntities();                        /*  第一種刪除方式            var userInfoList = from u in db.UserInfo                               where u.ID == 345                               select u;            UserInfo userInfo=userInfoList.FirstOrDefault(); //返回第一個元素,如果沒有的話,返回預設值null            if (userInfo != null)            {                //db.UserInfo.Remove(userInfo); //添加刪除標記,但是必須先執行查詢操作才能成功打上標記。                db.Entry<UserInfo>(userInfo).State = System.Data.EntityState.Deleted; //添加刪除標記,此方法不需要執行查詢就可以直接打上標記。                db.SaveChanges();  //執行刪除操作。            }            else            {                Response.Write("要刪除的資料不存在!!");            }            */            //第二種刪除方式。            UserInfo userInfo = new UserInfo() {ID=344};            //db.UserInfo.Remove(userInfo); //先執行查詢操作,才能打上標記(不常用)。            db.Entry<UserInfo>(userInfo).State = System.Data.EntityState.Deleted;  //添加刪除標記,此方法不需要執行查詢就可以直接打上標記。            db.SaveChanges(); //如果要刪除的資料不存在,則會報錯。刪除之前最好查詢一下。        }        //修改        protected void Button4_Click(object sender, EventArgs e)        {            EFFristModelEntities 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("要修改的資料不存在!!");            }        }    }}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.