Asp.net MVC 简单入门

发布时间:2020-08-05 16:32:15 作者:ly_bing
来源:网络 阅读:426

年前一直想写个系列教程来完整的过一下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>());
}

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


推荐阅读:
  1. ASP.NET MVC5&微信公众平台整合开发实战(响应式布局、JQuery Mobile,Wind
  2. 在ASP.NET Core MVC中如何构建简单Web Api

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net mvc mvc

上一篇:DNS快速配置实验

下一篇:python3.6安装pip的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》