内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Asp.netMVC簡單入門-創(chuàng)新互聯(lián)

年前一直想寫個系列教程來完整的過一下Asp.NET MVC,同時也可以幫助一些人來避免我在工作中遇到的坑,碰過的壁。緣于能力有限,時間也不充足,一直也沒有能實現(xiàn),幸好看到 Marla Sukesh 寫了個7天教程,講的挺好,就想順便翻譯過來給各位,英文水平有限,請各位多多包涵。

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的瀏陽網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

菜鳥,請主動動手,不段動手才會發(fā)現(xiàn)問題。

大神,請留下寶貴的看法。

有問題或建議盡管提。

今天先簡單用”Code First”的模式基于 EF5, MVC4 和 MVC Scaffolding(腳手架->通過Nuget安裝)創(chuàng)建一個簡單的圖書管理系統(tǒng)來熱身, 數(shù)據(jù)庫我們就選擇微軟自家的SQL Server2012了:

環(huán)境如下: Win7 with sp1 and VS2012Asp.net MVC 簡單入門

Asp.net MVC 簡單入門

Asp.net MVC 簡單入門

Asp.net MVC 簡單入門

步驟1:創(chuàng)建一個以Razor為引擎的互聯(lián)網(wǎng)應(yīng)用程序:

Asp.net MVC 簡單入門

Asp.net MVC 簡單入門

步驟2:安裝EntityFramework

Asp.net MVC 簡單入門

安裝EntityFramework:

PM> Install-Package EntityFramework

安裝MvcScaffolding

PM> Install-Package MvcScaffoldingAttempting to resolve dependency 'T4Scaffolding'.Attempting to resolve dependency 'T4Scaffolding.Core'.Attempting to resolve dependency 'EntityFramework'.Installing 'T4Scaffolding.Core 1.0.0'.Successfully installed 'T4Scaffolding.Core 1.0.0'.Installing 'T4Scaffolding 1.0.8'.Successfully installed 'T4Scaffolding 1.0.8'.Installing 'MvcScaffolding 1.0.9'.Successfully installed 'MvcScaffolding 1.0.9'.

既然是code first,那么下來自然就是要來創(chuàng)建Models了:

在Models的文件夾中創(chuàng)建三個實體類:

/// <summary>
/// Book Entity
/// </summary>
public class Book
{
    [Key]
    public int ID { get; set; }
    public string BookName { get; set; }
    /// <summary>
    /// One to One
    /// </summary>
    public int PublisherID { get; set; }
    [ForeignKey("PublisherID")]
    public virtual Publisher Publisher { get; set; }
    /// <summary>
    /// One to Many
    /// </summary>
    public virtual ICollection<Author> Authors { get; set; }
}
/// <summary>
/// Author Entity
/// </summary>
public class Author
{
    [Key]
    public int ID { get; set; }
    public string AuthorName { get; set; }
    public int BookID { get; set; }
}
/// <summary>
/// Publisher Entity
/// </summary>
public class Publisher
{
    [Key]
    public int ID { get; set; }
    public string PublisherName { get; set; }
}

建三個實體類對應(yīng)的Mvc View---空View就好.

然后打開Package Manager Console, 運行下面的命令:

PM> Scaffold Controller Book
PM> Scaffold Controller Author
PM> Scaffold Controller Publisher

如果要通過Repository訪問數(shù)據(jù)那么要在最后加上一個參數(shù):–Repository

像:

PM> Scaffold Controller Book –Repository

如果要重新生成對應(yīng)的view和Controller那么再加一個參數(shù):-Force

像:

PM> Scaffold Controller Book –Repository –Force

然后你會得到的結(jié)果如下:

Asp.net MVC 簡單入門

神奇吧,自動就幫你生成了通用部分的View,與數(shù)據(jù)庫交互的Repository, Controller以及比較重要的FirstMouseContext,省心省力。FirstMouseContext

我們來配置下讓自動創(chuàng)建的東西,顯示出來,編輯Shared/_Layout.cshtml,添加一個鏈接進去,這樣好導(dǎo)航到我們view里, 如:

<li>@Html.ActionLink("Books", "Index", "Books")</li>
<li>@Html.ActionLink("Authors", "Index", "Authors")</li>
<li>@Html.ActionLink("Publishers", "Index",  "Publishers")</li>

得到了下面的錯誤信息:

Asp.net MVC 簡單入門

我們修改外鍵為可空,重新跑一次:

PM> Scaffold Controller Book -Repository -Force
PM> Scaffold Controller Author -Repository -Force
PM> Scaffold Controller Publisher -Repository –Force

運行起來,又錯了:

Asp.net MVC 簡單入門

明顯的忘記配置數(shù)據(jù)庫信息了,找到配置文件Web.config

<add name="DefaultConnection" connectionString="….”>

修改為:

<add name="FirstMouseContext" connectionString="….”>

再次啟動,正常了:

Asp.net MVC 簡單入門

Asp.net MVC 簡單入門

還有一點兒要捎帶注意的是 DbContext:

public class FirstMouseContext : DbContext
{
    // You can add custom code to this file. Changes will not be  overwritten.
    //
    // If you want Entity Framework to drop and regenerate your database
    // automatically whenever you change your model schema, add the  following
    // code to the Application_Start method in your Global.asax file.
    // Note: this will destroy and re-create your database with every model  change.
    //
    // System.Data.Entity.Database.SetInitializer(new  System.Data.Entity.DropCreateDatabaseIfModelChanges<FirstMouse.Models.FirstMouseContext>());
    public DbSet<FirstMouse.Models.Book> Books { get; set; }
    public DbSet<FirstMouse.Models.Author> Authors { get; set; }
    public DbSet<FirstMouse.Models.Publisher> Publishers { get; set;  }
}

看到解釋了吧?大概意思是說,如果你的Model的Schema有變化的時侯,如果想要重新生成數(shù)據(jù)庫,那么就應(yīng)該把下面的一句加在Global.asax文件里:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
        System.Data.Entity.Database.SetInitializer(new  System.Data.Entity.DropCreateDatabaseIfModelChanges<FirstMouse.Models.FirstMouseContext>());
    }
}

實際上你也可以放在構(gòu)造函數(shù)里:

public FirstMouseContext()
{
    System.Data.Entity.Database.SetInitializer(new  System.Data.Entity.DropCreateDatabaseIfModelChanges<FirstMouse.Models.FirstMouseContext>());
}

很好很強大吧?準備好精力迎接下周的密集知識點兒轟炸吧…

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

分享題目:Asp.netMVC簡單入門-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://www.rwnh.cn/article6/csjpig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、搜索引擎優(yōu)化標簽優(yōu)化、網(wǎng)站內(nèi)鏈、網(wǎng)站制作、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營
嘉黎县| 云龙县| 马关县| 象州县| 巩义市| 绵阳市| 武义县| 天水市| 偃师市| 辽阳县| 璧山县| 靖边县| 石棉县| 佛山市| 兴义市| 丰顺县| 东乌| 濮阳县| 巩留县| 琼结县| 嘉禾县| 伊宁县| 湖北省| 通海县| 会泽县| 昌吉市| 武义县| 称多县| 行唐县| 六安市| 阿坝| 新河县| 卢湾区| 宜章县| 襄垣县| 小金县| 定边县| 安阳县| 永修县| 航空| 澄城县|