Asp.netMVC简单入门-创新互联

年前一直想写个系列教程来完整的过一下Asp.NET MVC,同时也可以帮助一些人来避免我在工作中遇到的坑,碰过的壁。缘于能力有限,时间也不充足,一直也没有能实现,幸好看到 Marla Sukesh 写了个7天教程,讲的挺好,就想顺便翻译过来給各位,英文水平有限,请各位多多包涵。

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站制作、成都网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的浏阳网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

菜鸟,请主动动手,不段动手才会发现问题

大神,请留下宝贵的看法。

有问题或建议尽管提。

今天先简单用”Code First”的模式基于 EF5, MVC4 和 MVC Scaffolding(脚手架->通过Nuget安装)创建一个简单的图书管理系统来热身, 数据库我们就选择微软自家的SQL Server2012了:

环境如下: Win7 with sp1 and VS2012Asp.net MVC 简单入门

Asp.net MVC 简单入门

Asp.net MVC 简单入门

Asp.net MVC 简单入门

步骤1:创建一个以Razor为引擎的互联网应用程序:

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,那么下来自然就是要来创建Models了:

在Models的文件夹中创建三个实体类:

/// <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; }
}

建三个实体类对应的Mvc View---空View就好.

然后打开Package Manager Console, 运行下面的命令:

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

如果要通过Repository访问数据那么要在最后加上一个参数:–Repository

像:

PM> Scaffold Controller Book –Repository

如果要重新生成对应的view和Controller那么再加一个参数:-Force

像:

PM> Scaffold Controller Book –Repository –Force

然后你会得到的结果如下:

Asp.net MVC 简单入门

神奇吧,自动就帮你生成了通用部分的View,与数据库交互的Repository, Controller以及比较重要的FirstMouseContext,省心省力。FirstMouseContext

我们来配置下让自动创建的东西,显示出来,编辑Shared/_Layout.cshtml,添加一个链接进去,这样好导航到我们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 简单入门

明显的忘记配置数据库信息了,找到配置文件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有变化的时侯,如果想要重新生成数据库,那么就应该把下面的一句加在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>());
    }
}

实际上你也可以放在构造函数里:

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

很好很强大吧?准备好精力迎接下周的密集知识点儿轰炸吧…

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享题目:Asp.netMVC简单入门-创新互联
网站路径:https://www.cdcxhl.com/article6/csjpig.html

成都网站建设公司_创新互联,为您提供网站维护搜索引擎优化标签优化网站内链网站制作网站改版

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营