排球计分(七)—— 使用EF框架,创建Controller,生成数据库

在使用EF框架之前,我们需要写好模型类。然后在创建controller。

 在之前的博客中,我们已经设计,和完成了模型类,这时候

我们只需把代码拿过来就可以使用了。

Balls.cs

 

    namespace 排球计分程序.Models
    {
        public class Ball
        {
            public int ID { get; set; }
            public string XingMing { get; set; }
            public string Age { get; set; }
            public string WeiZhi { get; set; }
            public string Shenglv { get; set; }
            public string RongYu { get; set; }

 

        }
        public class BallDBContext : DbContext
        {
            public DbSet<Ball> Movies { get; set; }

 

        }
    }

AnnounceInfo.cs

namespace 排球队伍公告信息.Models.Entities
{
    public class AnnounceInfo
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public string DuiMing { get; set; }
        public string Jieshao { get; set; }
        public int Category { get; set; }
    }
}

 CategoryInfo.cs

 

namespace 排球队伍公告信息.Models.Entities
{
    public class CategoryInfo
    {
      

 

        public string Name { get; set; }

 

        public int ID { get; set; }
    }
}

Team.cs

namespace 排球计分.Models
{
    public class Team
    {
            public int ID { get; set; }//ID号
            public string Name { get; set; }//队名
            public decimal Score { get; set; }//得分
                   
            public string   Team { get; set; }
            public string  Score { get; set; }
            public string  Add { get; set; }
            public string Delete { get; set; }
            public string Faqiu { get; set; }
            public string Lanwang  { get; set; }
            public string Kouqiu { get; set; }
            public string Zhugong { get; set; }
            
 }

}

模型类创建完成后需要在Web.config文件中添加链接字符串:

 

<connectionStrings>
    <add name="BallDBContext"
    connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Balls.mdf;Integrated Security=True"
    providerName="System.Data.SqlClient"

 

/>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-排球计分程序-20170617163815;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-排球计分程序-20170617163815.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>

然后从控制器访问数据模型:
· 控制器(Controller)名称输入: BallsController.

· 模型类(Model class)选择: Balls (排球计分程序.Models) .

· 数据上下文类(Data context class)选择:BallsDBContext (排球计分程序.Models)
· 在新的Views\Movies文件夹中创建Create.cshtml、 Delete.cshtml、 Details.cshtml、 Edit.cshtml和Index.cshtml 文件。

Visual Studio自动创建 CRUD(创建、 读取、 更新和删除) 操作方法,和相关的视图文件(CRUD 自动创建的操作方法和视图文件被称为 scaffolding)。 现在您有了可以创建、列表、 编辑和删除排球运动员所有的Web功能了。

运行应用程序,通过将/Balls追加到浏览器地址栏 URL的后面,从而浏览Movies控制器。因为应用程序依赖于默认路由 ( App_Start\RouteConfig.cs 文件中的定义),浏览器请求http://localhost:xxxxx/Movies将被路由到Balls控制器默认的Index 操作方法。换句话说,浏览器请求http://localhost:xxxxx/Balls等同于浏览器请求http://localhost:xxxxx/Movies/Index

我们可以看到系统为我们生成的文件。然后我们可以对这些文件做操作;

点击解决方案的"显示全部文件",会显示出根据模型生成的数据库:

 到这里,我们就使用EF框架,完成了通过模型类,然后创建controller,进行数据库的连接,与访问。

在使用EF框架的情况下,数据库的连接是非常方便和好用的。

posted @ 2017-06-22 11:27  孙栋梁  阅读(203)  评论(0编辑  收藏  举报