系列文章:
EF-ModelFirst實(shí)現(xiàn)過(guò)程+數(shù)據(jù)庫(kù)遷移???? http://www.reibang.com/p/2a53f318144d
EF-DBFirst實(shí)現(xiàn)過(guò)程??? http://www.reibang.com/p/eb84ec814926
建議學(xué)習(xí)路徑 DBFirst->ModelFirst->CodeFirst
思路:通過(guò)代碼編寫實(shí)體和關(guān)聯(lián)次氨,編寫上下文蚀苛,根據(jù)實(shí)體和關(guān)聯(lián)生成數(shù)據(jù)庫(kù)禁荸。
EF-CodeFirst的優(yōu)點(diǎn)
EF-CodeFirst相對(duì)于EF-ModelFirst和EF-DBFirst的有點(diǎn)在于菜秦,CodeFirst在數(shù)據(jù)庫(kù)已經(jīng)設(shè)計(jì)完成之后连锯,能夠較方便得再次對(duì)數(shù)據(jù)庫(kù)進(jìn)行更改(數(shù)據(jù)庫(kù)遷移技術(shù))撮竿。
關(guān)聯(lián)
1.Database First是基于已存在的數(shù)據(jù)庫(kù)泉手,利用某些工具(如VS提供的EF設(shè)計(jì)器)創(chuàng)建實(shí)體類铣口,數(shù)據(jù)庫(kù)對(duì)象與實(shí)體類的匹配關(guān)系等滤钱,你也可以手動(dòng)修改這些自動(dòng)生成的代碼及匹配文件。也就是從一個(gè)數(shù)據(jù)庫(kù)開始脑题,然后生成實(shí)體框架和相應(yīng)代碼件缸。
??? 2.Model First 是先利用某些工具(如VS的EF設(shè)計(jì)器)設(shè)計(jì)出可視化的實(shí)體數(shù)據(jù)模型及他們之間的關(guān)系,然后再根據(jù)這些實(shí)體叔遂、關(guān)系去生成數(shù)據(jù)庫(kù)對(duì)象及相關(guān)代碼文件他炊。
??? 3.Code First 這種方式需要先寫一些代碼,如實(shí)體對(duì)象已艰,數(shù)據(jù)關(guān)系等痊末,然后根據(jù)已有的代碼描述,自動(dòng)創(chuàng)建數(shù)據(jù)對(duì)象哩掺。但其實(shí)這種方法與Model First是非常類似的凿叠。我們自己寫的代碼,其實(shí)就是用代碼表示實(shí)體模型嚼吞,而Model First是用可視化的方式描述了實(shí)體模型盒件。
EF-CodeFirst的具體實(shí)現(xiàn)過(guò)程(以17-3-2課后作業(yè)為對(duì)象分析)
Step1:按需求新建若干實(shí)體Entity
BaseEntity基類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_CodeFirst_17_3_2.Model
{
??? public class BaseEntity
??? {
??????? public BaseEntity()
??????? {
??????????? CreateTime = DateTime.Now;
??????????? UpdateTime = DateTime.Now;
??????? }
??????? /// <summary>
??????? /// 創(chuàng)建ID
??????? /// </summary>
??????? public int ID { get; set; }
??????? /// <summary>
??????? /// 創(chuàng)建人
??????? /// </summary>
??????? public string CreateBy { get; set; }
??????? /// <summary>
??????? /// 創(chuàng)建時(shí)間
??????? /// </summary>
??????? public DateTime CreateTime { get; set; }
??????? /// <summary>
??????? /// 更新人
??????? /// </summary>
??????? public string UpdateBy { get; set; }
??????? /// <summary>
??????? /// 更新時(shí)間
??????? /// </summary>
??????? public DateTime UpdateTime { get; set; }
??? }
}
Classes班級(jí)實(shí)體
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_CodeFirst_17_3_2.Model
{
?? public class Classes:BaseEntity
??? {
??????? /// <summary>
??????? /// 班級(jí)編號(hào)
??????? /// </summary>
??????? public int ClassNO { get; set; }
??????? /// <summary>
??????? /// 班級(jí)名稱
??????? /// </summary>
??????? public string ClassName { get; set; }
??????? /// <summary>
??????? /// 班主任ID
??????? /// </summary>
??????? public int MentorID { get; set; }
??????? /// <summary>
??????? /// 班主任名稱
??????? /// </summary>
??????? public virtual string MentorName { get; set; }
??????? /// <summary>
??????? /// 一個(gè)班級(jí)有多名學(xué)生
??????? /// </summary>
?????? public virtual List<Student> students { get; set; }
??? }
}
Course課程實(shí)體
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_CodeFirst_17_3_2.Model
{
??? public class Course:BaseEntity
??? {
??????? /// <summary>
??????? /// 課程編號(hào)
??????? /// </summary>
??????? public int CourseNO { get; set; }
??????? /// <summary>
??????? /// 課程名稱
??????? /// </summary>
??????? public string CourseName { get; set; }
??????? /// <summary>
??????? /// 一個(gè)課程有多名學(xué)生
??????? /// </summary>
??????? public virtual List<Student> students { get; set; }
??????? /// <summary>
??????? /// 一門課程有多個(gè)教師
??????? /// </summary>
??????? public virtual List<Teacher> Teachers { get; set; }
??? }
}
學(xué)校School實(shí)體
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_CodeFirst_17_3_2.Model
{
??? public class School : BaseEntity
??? {
??????? /// <summary>
??????? /// 學(xué)校編號(hào)
??????? /// </summary>
??????? public int SchoolNO { get; set; }
??????? /// <summary>
??????? /// 學(xué)校名稱
??????? /// </summary>
??????? public string SchoolName { get; set; }
??????? /// <summary>
??????? /// 校長(zhǎng)ID
??????? /// </summary>
??????? public int HeadmasterID { get; set; }
??????? /// <summary>
??????? /// 校長(zhǎng)名稱
??????? /// </summary>
??????? public string HeadmasterName { get; set; }
??????? /// <summary>
??????? /// 學(xué)校地址
??????? /// </summary>
??????? public string Address_new { get; set; }
??? }
}
學(xué)生Student實(shí)體
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_CodeFirst_17_3_2.Model
{
??? public class Student:BaseEntity
??? {
??????? /// <summary>
??????? /// 學(xué)生編號(hào)
??????? /// </summary>
??????? public int StudentNO { get; set; }
??????? /// <summary>
??????? /// 學(xué)生名稱
??????? /// </summary>
??????? public string StudentName { get; set; }
??????? /// <summary>
??????? /// 一個(gè)學(xué)生屬于一個(gè)班級(jí)名稱
??????? /// </summary>
??????? public virtual Classes ClassName { get; set; }
??????? /// <summary>
??????? /// 一個(gè)學(xué)生有多門課程
??????? /// </summary>
??????? public virtual List<Course> Courses { get; set; }
??? }
}