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的文件夹中创建三个实体类:

/// 
/// Book Entity
/// 
public class Book
{
    [Key]
    public int ID { get; set; }
    public string BookName { get; set; }
    /// 
    /// One to One
    /// 
    public int PublisherID { get; set; }
    [ForeignKey("PublisherID")]
    public virtual Publisher Publisher { get; set; }
    /// 
    /// One to Many
    /// 
    public virtual ICollection Authors { get; set; }
}
/// 
/// Author Entity
/// 
public class Author
{
    [Key]
    public int ID { get; set; }
    public string AuthorName { get; set; }
    public int BookID { get; set; }
}
/// 
/// Publisher Entity
/// 
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里, 如:

  • @Html.ActionLink("Books", "Index", "Books")
  • @Html.ActionLink("Authors", "Index", "Authors")
  • @Html.ActionLink("Publishers", "Index",  "Publishers")
  • 得到了下面的错误信息:

    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

    修改为:

    再次启动,正常了:

    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());
        public DbSet Books { get; set; }
        public DbSet Authors { get; set; }
        public DbSet 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());
        }
    }

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

    public FirstMouseContext()
    {
        System.Data.Entity.Database.SetInitializer(new  System.Data.Entity.DropCreateDatabaseIfModelChanges());
    }

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


    网站标题:Asp.netMVC简单入门
    文章出自:http://ybzwz.com/article/gohped.html