您的位置:首页 > 编程语言 > ASP

[Asp.net MVC]Asp.net MVC5系列——添加模型

2014-08-04 22:19 417 查看

目录

概述

添加模型

总结

系列文章

[Asp.net MVC]Asp.net MVC5系列——第一个项目

[Asp.net MVC]Asp.net MVC5系列——添加视图

概述

在本节中我们将追加一些类来管理数据库中的学生信息。这些类将成为我们的MVC应用程序中的“模型”部分。

在vs2013中EF的版本为(Entity Framework)EF6,我们将使用EF6来进行对学生信息的维护,顺便也学习一下EF6的增删改查。

添加模型

在解决方案资源管理器中,鼠标右击Models文件夹,点击“添加”菜单下的“新建项”,如图所示。





我这有个学生信息的测试数据库,所以选择了从数据库生成,为了以后方便使用,也懒得去新建数据库了。





选择新建连接



选择“是,在连接字符串中包括敏感数据”,如果选择上面的则连接字符串中密码是以一串“*”替换的,你还得修改,所以怎么简单怎么来了。



选择ef版本,这里选择ef6



选择数据库对象,这里可能要用到student,score,course表,所以将选择三张数据表。



选择数据表后,单击完成,后会弹出一个安全警告的对话框,直接确定忽视它。



之后会弹出三张表的关系图



到目前为止我们创建了三个模型类Student,Score,Course类



具体代码如下:

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码已从模板生成。
//
//     手动更改此文件可能导致应用程序出现意外的行为。
//     如果重新生成代码,将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------

namespace Wolfy.FirstMVCProject.Models
{
using System;
using System.Collections.Generic;

public partial class Student
{
public Student()
{
this.Score = new HashSet<Score>();
}

public int stuId { get; set; }
public string stuName { get; set; }
public string stuSex { get; set; }
public System.DateTime stuBirthdate { get; set; }
public System.DateTime stuStudydate { get; set; }
public string stuAddress { get; set; }
public string stuEmail { get; set; }
public string stuPhone { get; set; }
public Nullable<bool> stuIsDel { get; set; }
public Nullable<System.DateTime> stuInputtime { get; set; }
public int classId { get; set; }

public virtual Course Course { get; set; }
public virtual ICollection<Score> Score { get; set; }
}
}


//------------------------------------------------------------------------------
// <auto-generated>
//     此代码已从模板生成。
//
//     手动更改此文件可能导致应用程序出现意外的行为。
//     如果重新生成代码,将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------

namespace Wolfy.FirstMVCProject.Models
{
using System;
using System.Collections.Generic;

public partial class Course
{
public Course()
{
this.Score = new HashSet<Score>();
this.Student = new HashSet<Student>();
}

public int classId { get; set; }
public string className { get; set; }
public string classDescription { get; set; }

public virtual ICollection<Score> Score { get; set; }
public virtual ICollection<Student> Student { get; set; }
}
}


//------------------------------------------------------------------------------
// <auto-generated>
//     此代码已从模板生成。
//
//     手动更改此文件可能导致应用程序出现意外的行为。
//     如果重新生成代码,将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------

namespace Wolfy.FirstMVCProject.Models
{
using System;
using System.Collections.Generic;

public partial class Score
{
public int testId { get; set; }
public int stuId { get; set; }
public int classId { get; set; }
public int testBase { get; set; }
public int testBeyond { get; set; }
public int testPro { get; set; }
public string testName { get; set; }
public System.DateTime testDate { get; set; }

public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
}


在下篇文章中,我们将要创建一个新的SchoolController类,用来显示数据库中的数据,并且允许用户创建学生列表,可以添加学生信息。

总结

在网上看了很多类似的文章都是使用code-first的方式,如果再写一个code-first类似的文章,老花样去玩,没意思,玩玩新的东西还是有必要的。所以既然vs2013中出现了新的玩法,何不尝试一下?

本文关于添加模型的内容较少,大量篇幅说了ef,因为添加模型不知道该说什么?添加一个类,该如何说啊?发愁!

如果你的眼睛比较锋利,也许会发现,从添加ado.net实体模型生成的文件有*.tt的文件,你懂得!这东西也可以研究研究。

参考文章:

http://www.asp.net/mvc/tutorials/mvc-5/introduction/adding-a-model
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: