




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
ASP.NetMVC2.0ASP.NETMVC的基本知識SqlServer2008ExpressASP.NETMVC2.0VS2008安裝MVC2.0官網:4、降臨ASP.NetMVC3.05、基于ASP.NetMVC3.0MusicStore6、ASP.NetMVC心得體會是微軟的一個ORM框架O/RMapEF1、新建一個ConsoleApplication,命名為EF2、打開數據庫,新建數據庫House,Home(Id,intidentity(1,1PRIMARY;Name,nvarchar(32);Address,nvarchar(64)) House.edmx文件和House.Designer.cs文件界面轉向edmx設計器(new一個HouseEntitiesHome,上下文和實體中定義的類)staticvoidMain(string[]args){HouseEntitieshouseEntities=newHouseEntities();Homehome=newHome();}{HouseEntitieshouseEntities=newHouseEntities();Homehome=newHome();home.Name=“海景別墅home.Address=“金和}staticvoidMain(string[]{HouseEntitieshouseEntities=newHouseEntities();Homehome=newHome();home.Name海景別墅home.Address=“金和”;}后對上下文操作,將home對象數據添加到網關對應數據中然后調用SaveChanges方法“保存”數據,將數據數據庫 1、添加Linq語句(Home是一個集合)varresult=fromhinselectforeach(variteminresult){}varhome1=fromhinhouseEntities.Homewhereh.Id==2selectvarhome1=(fromhinhouseEntities.Homewhereh.Id==selecth).SingleOrDefault<Home>();if(home!={home1.Name=}c SaveChanges方法做了什可用通過SQLServerSQLprofiler在對象模型中:右鍵->表,可以查看類與數據表的對應關微軟圖解(ORM的整體概念 1、新建一個ConsoleApplicationEFModel2、添加一個數據模型School.edmx4->實體(Student)->修改實體集(Student)->6、在模型上右鍵添加一個字段修改Name屬性中的類型,注意string類型需要設定最大長度,默認MAX8->->->生成edmx.sql9、edmx.sqlSQL->SQL->->staticvoidMain(string[]{SchoolContainerschoolContainer=newSchoolContainer();Studentstu1=newStudent();Studentstu2=newStudent();stu1.Name=“s1”;stu2.Name=“s2”;}{SchoolContainerschoolContainer=newSchoolContainer();Studentstu1=newStudent();Studentstu2=newStudent();stu1.Name=“s1”;stu2.Name=“s2”;varstu=SchoolContainer.Student.Where<Student>(s=>s.Id==}注:Single與SingleOrDefault的區別在于Single在沒有結果錯這里不直接使用前面的語法,寫個Lambda表達式完成查詢->->->選擇附加{{}}{Console.Wri}5、另外兩個方法.和. {varstu=if(stu!={}}IQueryable接口與IEnumberable接口(使用SQLServerProfiler工具進行2、IQueryable是一個復雜的離線的版本,當在使用的時候才會去數據庫中去數據varresults=(fromhinhouseEntities.HomeselectvarresultIQueryable=(fromhinvarresultIEnumberable=(fromhinhouseEntities.Homeselect/flySQLServerProfiler1、工具->SQLServerProfiler->連接所要的數據庫->設置屬性(一般直接運行LinqToEF(一對多的關系)School.edmx->->->填充屬性->->->標量屬性(Name)->->->->選擇對應類型(1-1,1-n,n-1,n-n)選擇Student-Course(1-n)->->SQLstaticvoidMain(string[]args){Studentstu=newStudent();stu.Name=Coursecourse=newcourse.Name宇宙學}staticvoidMain(string[]args){SchoolContainerschoolContainer=newSchoolContainer();Studentstu=newStudent();stu.Name=CoursecoursenewCourse();course.Name=“宇宙學”;};//{SchoolContainerschoolContainer=newSchoolContainer();Studentstu=newStudent();stu.Name=CoursecoursenewCourse();course.Name=“宇宙學”;}{SchoolContainerschoolContainer=newSchoolContainer();Coursecourse=newCourse();course.Name=“宇宙學2”;course.StudentId=1;}EF執行的時候,默認就會加一個比較低級的事務App.config中存放的還有Key,可以傳入KeyKey選擇連接字符串varresult=fromsinschoolContainer.Studentwheres.Id==1wheres.Course.Count>1selects.Course;{}var<resultName>=<dontainer>.<TableType>.Where<TableType>(<Lambda3、由于上下文的緩存,盡可能的復用,同時由于該因素,上下文會越來越大。WinForm中比較難處理該情況,在Web中可以對Http請求進行分組,每一組new一個上下文EF也是跨數據庫支持的
/flyWebForm方式ASP.NetMVC一般常用架構:MVCEFWCF3、可以控制生成自定義的URLASP.NetMVC2、Model主要是或是處理數據的組件4、ControllerMVC請求模型用WebForm中模擬MVCCodeBehind=”Default.aspx.cs”Inherits=”WE._Default”%><asp:LabelID=”Label1”runat=”server”<%=MyNamepublicpartialclass_Default:{privatestringmyName;publicstring{get{returnmyName;}set{myName=value;}{this.Label1.Text=“oMVC”;MyName=“nihao!”;}}MyNameModel,將Page_Load看作ControllerWebForm的開發前代碼緊耦合,修改需要前后匹配publicpartialclass_Default:{publicstringMyName{get{returnmyName;}set{myName=value;}publicList<string>Names=newList<string>(){MyName=“nihao!”;}}CodeBehind=”Default.aspx.cs”Inherits=”WE._Default”%><%=MyName<%for(inti=0;i<Names.Count;{<br<span><%=Names[i]<%} CodeBehind=”Default.aspx.cs”Inherits=”WE._Default”%><%=MyName<%for(inti=0;i<Names.Count;{<br<span><%=Names[i]%></span><span><%:Names[i]<%}publicpartialclass_Default:{publicstringMyName{get{returnmyName;}set{myName=value;}publicList<string>Names=newList<string>(){MyName=“nihao!”;}} 因此<%:Names[i]%>等價于<%=HttpUtility.HtmlEncode(Names[i])MVC的第一個項目 2->3Controllers->->這是命名約定,命名為TeacherController,選擇添加usingusingSystem.Linq;usingSystem.Web;namespaceMvcApp{{//Get:{}}}在Index方法上右鍵->添加視圖->彈出一個里面包含一個Index.aspx文件<%@PageTitle=””Language=”C#”MasterPageFile=”~/Views/Shared/Site.Master”Inherits=”System.Web.Mvc.Viege<dynamic>”%>在MVC中,請求到Controller中時,系統會判斷是那個類的方法publicclass{publicstringId{get;set;}publicstringName{get;set;}publicstringAddress{get;set;}}{{teacher.Address=“nhao”;teacher.Id=“1”;teacher.Name=“malun”;returnView();}}{{teacher.Id=“1”;teacher.Name=“malun”;ViewData[“Teacher1”]=teacher;}}運行returnView()ViewData中的數據交給View<%@PageTitle=””Language=”C#”MasterPageFile=”~/Views/Shared/Site.Master”Inherits=”System.Web.Mvc.Viege<dynamic>”%><%:ViewData[“Teacher1”] publicViewDataDictionary<TModel>ViewData{get;set;在執行View()方法的時候,交給ViewpublicclassTeacherController:{{teacher.Address=“nhao”;teacher.Id=“1”;teacher.Name=“malun”;ViewData[“Teacher1”]=teacher;returnView(“MyPage”);}}注:請求執行過程,會在URL指定的路徑下找相應文件和方法如果不存在,則在Shared文件夾下去找即URL后的名字與方法名相同<%@PageTitle=””Language=”C#”MasterPageFile=”~/Views/Shared/Site.Master” ge<MvcApp.Models.Teacher>”%>地址:Model.Address:<%:Model.NamepublicActionResultIndex(){…ViewData.Model=teacher;returnView();}2、每個Controller都對應ViewControllerMVC的模式下,與WebForm1、新建Controller->創建2、根據Action創建View->在Action獲取數據產生ActionResult傳遞給View注:View顯示數據的模板請求流程:urlController.ActionView1、View和ControllerModelModel不依View和Controller2、Controller可以直接請求View來顯示具體頁面,View不依賴與ControllerModels->->數據:ADO.NET實體模型(HouseCtrCtCtr8、在Index方法上添加視圖 …<%foreach(variteminModel){
<%:item.ID<%:item.CPU<%:item.Mouse<%}…{HouseContainerhouseContext=newViewData.Model= returnView();}->選擇屬性(強類型、視圖內容:Details)->aspx{HouseContainerhouseContext=newvarresult=fromcin puterwherec.ID==idselectc;ViewData.Model=result.SingleOrDefault<Computer>();}{varresult=fromcin puterwherec.ID==idselectc;returnView(result.SingleOrDefault<Computer>());}publicActionResult{}當提交數據的時候,使用post{{}
{}}15、為Createcreate->->選擇視圖內容(Create)->form
<%}實際上可以通過Request方法來找到提交的數據,例如{…Request[“input”];//的name…}{… //屬性…}publicActionResultCreate(Computer{{}
HouseContainerhouseContextnewHouseContainer();//new一個網{}}16、下面做代碼與Details代碼幾乎相同{HouseContainerhouseContext=newvarresult=fromcin puterwherec.ID==idsel
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024-2025學年人教版(PEP)三下英語期末模擬卷(含答案含聽力原文無音頻)
- 《金融服務營銷》 測試題及答案A
- 工業廢水處理與排放標準環境監測研究
- 工業機器人應用及操作規范介紹
- 工業旅游開發與文化傳承研究
- 工業機器人技術及智能制造應用案例
- 工業污染防治與清潔生產技術
- 工業物聯網提升非標設備運行效率的策略
- 工業污染防治技術及措施
- 工業污染防治的技術與策略
- 預防艾梅乙母嬰傳播知識
- 總監述職報告
- 兒童意外傷害預防及家庭安全教育推廣研究報告
- 國家開放大學本科《當代中國政治制度》期末紙質考試總題庫2025春期版
- 滴滴網約車出行品牌-品牌視覺識別手冊【出行打車】【VI設計】
- 課題申報參考:中華體育精神鑄牢新疆青年學生中華民族共同體意識的時代價值和實踐路徑研究
- 臨床生化檢驗講課
- 生物制造產業園項目實施規劃與進度安排
- 蒸飯培訓課件
- 高校物業管理服務實施方案
- 工程造價咨詢服務投標方案(專家團隊版-)
評論
0/150
提交評論