ibcadmin 发表于 2019-12-20 08:49:28

ASP.NET MVC项目中EntityFramework"代码优先方法"的使用步骤

<p>EF提供了三种方式来实现项目,分别是:</p>
<p>(1)代码优先方法:</p>
<p>(2)模子优先方法:</p>
<p>(3)数据库优先方法:</p>
<p>本篇重要记载在Vs2010情况下使用<strong>代码优先的方式</strong>实现数据库和后端代码数据交互,语言为C#,数据库为sqlserver 2008。</p>
<p>1.在sqlserver中创建数据库 "StudentDB",注意只必要创建库名即可,不消建表;</p>
<p>2.打开VS2010,工具->毗连到数据库,选择服务器名、验证方式等,获取毗连字符串,注意毗连字符串的name属性,必要在后边做对应。如下:</p>
<p style="margin-left: 30px;"></p>
<p>3.使用Nuget大概引用dll,安装EntityFramework。</p>
<p>4.在mvc项目的model文件夹中创建简单的.NET类,并在该model下引用:using System.ComponentModel.DataAnnotations定名空间,为主键标.  </p>
<p style="margin-left: 30px;" align="left"></p>
<p> </p>
<p> </p>
<p>5.在根目次下新建Data Aceess Layer文件夹,新建类StudenDAL,该类名务必与web.config中毗连字符串name=“StudentDAL”同等,在该数据处置处罚类中添加引用using System.Data.Entity,并继承DbContext类:</p>
<p>   在StudentDAL类中重写OnModelCreating()方法代码,并引用model类using WebApplicaion.Models,然后添加public DbSet<Student> Students{set;get;}{},代码如下:</p>
<p style="margin-left: 30px;"></p>
<p style="margin-left: 30px;"> </p>
<p>6.在获取数据类大概controller中引用该数据处置处罚类 using WebApplication1.DataAccessLayer ; 然后编写获取数据方法:</p>
<p style="margin-left: 30px;"></p>
<p style="margin-left: 30px;"> </p>
<p>7.在controller中使用该方法获取数据,并将数据返回至view上:</p>
<p> </p>
页: [1]
查看完整版本: ASP.NET MVC项目中EntityFramework"代码优先方法"的使用步骤