星空网 > 软件开发 > ASP.net

简单的Code First例子(EF Code

现在假想,我们想要为讴歌学校创建一个应用程序,这个程序需要能够来添加或者更新学生,分数,教师还有课程信息。

代替之前我们的做法:先是创建数据库,现在我们不这么做,我们先来创建领域类,首先我来创建两个简单的类,一个是Student类,一个是Standard类。

每个学生都有一个分数,下面看代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF1{  public class Student  {    public int StudentId { get; set; }    public string StudentName { get; set; }    public DateTime DateOfBirth { get; set; }    public byte[] Photo { get; set; }    public decimal Height { get; set; }    public float Weight { get; set; }    public Standard Standard { get; set; }  }}

这个Standard(分数类)需要容纳下复数个学生(Student )

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace EF1{  public class Standard  {    public int StandardId { get; set; }    public string StandardName { get; set; }    public ICollection<Student> Students { get; set; }  }}

现在,我们已经为我们的程序完成了,领域类初始化的工作。Code-First方法,同样需要一个继承自DbContext的上下文类。了解更多上下文类的信息请点击: DbContext

 

创建上下文类的代码如下,我们这个上下文类,继承自DbContext类,然后暴露你想要的模型的实体的DbSet属性,例如,Student和Standard类,在这个例子中。DbSet是实体的集合(also konw as entity set),所以我们给这个实体的属性名字是复数的。

using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF1{  public class DbContextClass:DbContext  {    public DbContextClass()      : base()    { }    public DbSet<Student> Studnets { get; set; }    public DbSet<Standard> Standards { get; set; }  }}

现在我们已经创建了必须的领域类和上下文类,现在让我们使用上下文,来添加一个学生信息吧

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF1{  class Program  {    static void Main(string[] args)    {      Student stu = new Student() { StudentId = 1, StudentName = "韦小宝", Height = 172, Weight = 120 ,DateOfBirth=DateTime.Now};      using (var db = new DbContextClass())      {        db.Studnets.Add(stu);        db.SaveChanges();      }      Console.WriteLine("添加成功");      Console.ReadKey();    }  }}

运行的效果图:

简单的Code First例子(EF Code

运行成功了?那么你会奇怪,数据库呢,数据表呢,别奇怪,慢慢看:

This is the beauty of Code-First APIs of Entity Framework. It creates the database based on parameter passed in the base constructor of your context class. Since we have not passed any parameter in the constructor of our context class, it created "EF_Code_First_Tutorials.SchoolContext" database in the local SQLEXPRESS database, as shown below. It also created two tables in this database, Students and Standards tables based on Student and Standard domain classes defined above.

这就是Code-First APIs的优点,它基于上下文类中的base构造器中的参数,为我们创建了数据库。尽管,我们没有传递任何参数到构造器中,它为我们在这个目录下面【C:\Users\XXXXXX】创建了这个数据库:

简单的Code First例子(EF Code

注意:因为我的数据库版本不是免费版的,所以创建的数据库,在这个目录下面:【C:\Users\XXXXXX】

现在我想把这个数据库,附加到我的SQL中,查看里面的表,但是出错了:

简单的Code First例子(EF Code

版本不支持,我数据库的版本是SQL2008,这个数据库的版本,应该是比2008低级的版本。

 

在网上百度了一下:解决方案地址:http://www.2cto.com/database/201404/294024.html

“数据库 的版本为 661,无法打开。此服务器支持 655 版及更低版本。不支持降级路径”

出现这样的问题,一般是因为数据库版本不同造成的。

我们可以用下面的语句查询数据库的版本

use master

select @@VERSION

(1)661是sql2008 R2的版本号

Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) Apr 2 2010 15:53:02 Copyright (c) Microsoft

(2)655版本也就是sql2008 sp1版本号

Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86) Mar 29 2009 10:27:29 Copyright (c) 1988-2008

 

如果后面是1600,则表明只安装了sql2008r2,还要装一个sql2008sp1。这样才能保证后面是2500.

刚好我的数据库,是第一个版本。我来找一个SQL2008Sp1。

 

As you can see in the above figure, it has created Students and Standards tables and each table contains columns with appropriate datatype and length. The column names and datatype matches with the properties of the respective domain classes. It has also made StudentId and StandardId as PK (primary key) and Standard_StandardId column as FK (foreign key).

This way, without creating a database first, you can start writing an application that will eventually create the database from your domain classes.

You must be wondering how it has created columns with appropriate datatypes and lengh with PK & FK, right? The answer is, using code-first conventions.

Learn code-first conventions in the next section.




原标题:简单的Code First例子(EF Code

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Lazada发布2022年春节假期安排:https://www.goluckyvip.com/news/6064.html
注意!在Lazada/Shopee平台售卖这些产品的迅速下架:https://www.goluckyvip.com/news/6065.html
eBay物流SpeedPAK各路向运费下调通知:https://www.goluckyvip.com/news/6066.html
Shopee卖家中心即日起开始升级,影响28-36小时!:https://www.goluckyvip.com/news/6067.html
超详细全美国FBA仓库一览!亚马逊热门仓库有哪些?:https://www.goluckyvip.com/news/6068.html
Zalora发货怎么弄?Zalora平台物流有吗?:https://www.goluckyvip.com/news/6069.html
在古巴做游轮 古巴旅游项目:https://www.vstour.cn/a/363194.html
西藏旅游攻略自驾游需要多少天 去西藏旅游自驾游要多久时间:https://www.vstour.cn/a/363195.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流