新聞中心
Code First是指以編寫程序?yàn)橹行?,通過對(duì)象映射(ORM)技術(shù)自動(dòng)生成關(guān)系數(shù)據(jù)庫(kù)的方法。這種方法主要解決了程序員在開發(fā)過程中需要手動(dòng)編寫SQL語(yǔ)句、維護(hù)數(shù)據(jù)庫(kù)結(jié)構(gòu)的問題,并強(qiáng)調(diào)以代碼編寫為核心,實(shí)現(xiàn)更快速、靈活、高效的開發(fā)模式。

成都創(chuàng)新互聯(lián)公司的客戶來(lái)自各行各業(yè),為了共同目標(biāo),我們?cè)诠ぷ魃厦芮信浜希瑥膭?chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對(duì)我們的要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。專業(yè)領(lǐng)域包括成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、電商網(wǎng)站開發(fā)、微信營(yíng)銷、系統(tǒng)平臺(tái)開發(fā)。
隨著云計(jì)算、物聯(lián)網(wǎng)等技術(shù)的普及,數(shù)據(jù)的存儲(chǔ)與管理成為項(xiàng)目開發(fā)的重要組成部分。利用Code First技術(shù),開發(fā)者不僅可以輕松地創(chuàng)建個(gè)性化數(shù)據(jù)庫(kù),還可以更加方便地添加、修改、查詢數(shù)據(jù),并快速迭代、升級(jí)系統(tǒng)。
如何呢?下面我們通過實(shí)戰(zhàn)案例來(lái)說(shuō)明。
我們需要安裝相應(yīng)的軟件和工具包。Code First技術(shù)主要依賴于Entity Framework和Microsoft SQL Server和VS Studio這三種工具,因此需要確保這些軟件都已經(jīng)正確安裝。
我們需要考慮建立數(shù)據(jù)庫(kù)的數(shù)據(jù)結(jié)構(gòu)。這一過程基于對(duì)象關(guān)系映射技術(shù),將對(duì)象屬性與數(shù)據(jù)庫(kù)表字段一一對(duì)應(yīng)。
假設(shè)我們有一個(gè)簡(jiǎn)單的電商平臺(tái),需要建立商品類別表和商品表。商品類別有編號(hào)(CategoryID)、名稱(CategoryName)和描述(CategoryDesc),其中編號(hào)是主鍵;商品有編號(hào)(ProductID)、名稱(ProductName)、單位(ProductUnit)、價(jià)格(Price)和類別編號(hào)(CategoryID),其中編號(hào)是主鍵,類別編號(hào)是外鍵。
根據(jù)這一結(jié)構(gòu),我們可以編寫下面的C#代碼:
public class ProductCategory
{
[Key]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string CategoryDesc { get; set; }
public virtual ICollection Products { get; set; }
}
public class Product
{
[Key]
public int ProductID { get; set; }
public string ProductName { get; set; }
public string ProductUnit { get; set; }
public decimal Price { get; set; }
public int CategoryID { get; set; }
public virtual ProductCategory Category { get; set; }
}
在這里,我們定義了兩個(gè)類ProductCategory和Product,分別對(duì)應(yīng)商品類別和商品。通過使用C#屬性語(yǔ)法,我們定義了類的屬性,如主鍵、名稱、描述等。關(guān)鍵字[Key]用于定義主鍵,virtual關(guān)鍵字則表示該屬性為虛擬屬性,可以由框架自動(dòng)轉(zhuǎn)換為數(shù)據(jù)庫(kù)實(shí)體。其中,Product類的CategoryID屬性為外鍵,通過virtual關(guān)鍵字和一個(gè)ProductCategory對(duì)象關(guān)聯(lián)起來(lái),實(shí)現(xiàn)類與類之間的關(guān)系。
接下來(lái),我們需要在程序中生成數(shù)據(jù)庫(kù)。我們應(yīng)該在應(yīng)用程序啟動(dòng)時(shí)初始化數(shù)據(jù)上下文,用于創(chuàng)建和管理數(shù)據(jù)庫(kù)。我們可以通過將DbContext操作作為公共方法來(lái)實(shí)現(xiàn)數(shù)據(jù)庫(kù)操作。
public class MyContext : DbContext
{
public MyContext() : base(“name = MyContext”)
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
}
public DbSet Categories { get; set; }
public DbSet Products { get; set; }
}
在這里,我們定義了一個(gè)派生自DbContext的MyContext類。通過DbSet屬性,我們定義了一個(gè)表(即實(shí)體),包含了我們前面所定義的ProductCategory和Product對(duì)象。
在程序運(yùn)行時(shí),我們可以通過MyContext類來(lái)訪問數(shù)據(jù)庫(kù),并使用Linq語(yǔ)句操作數(shù)據(jù)。如我們可以通過下面的代碼來(lái)插入一條商品分類數(shù)據(jù):
MyContext context = new MyContext();
context.Categories.Add(new ProductCategory() { CategoryID = 1, CategoryName = “電腦配件”, CategoryDesc = “計(jì)算機(jī)附件” });
context.SaveChanges();
在這里,我們首先創(chuàng)建了一個(gè)MyContext對(duì)象,然后通過Add方法向Categories表中插入一條商品類別數(shù)據(jù)。調(diào)用SaveChanges方法進(jìn)行提交操作。
通過這樣的方式,我們便可以輕松地創(chuàng)建個(gè)性化的關(guān)系數(shù)據(jù)庫(kù),并通過程序?qū)ζ溥M(jìn)行操作和維護(hù)。Code First技術(shù)不僅提高了開發(fā)人員的開發(fā)效率,更為重要的是它也能讓我們將關(guān)注點(diǎn)放在程序本身的開發(fā)和實(shí)現(xiàn)上,而將數(shù)據(jù)庫(kù)的維護(hù)和管理等問題交由框架來(lái)解決,為開發(fā)人員提供了更好的開發(fā)體驗(yàn)。
相關(guān)問題拓展閱讀:
- 電腦基本英語(yǔ)單詞有哪些
電腦基本英語(yǔ)單詞有哪些
電腦基本英語(yǔ)單詞
CPU(Center Processor Unit)中央處理單元
mainboard主板
RAM(random access
memory)隨機(jī)存儲(chǔ)器(內(nèi)存)
ROM(Read Only Memory)只讀存儲(chǔ)器
Floppy Disk軟盤
Hard Disk硬盤
CD-ROM光盤驅(qū)動(dòng)器(光驅(qū))
monitor監(jiān)視器
keyboard鍵盤
mouse鼠標(biāo)
chip芯片
CD-R光盤刻錄機(jī)
HUB集線器
Modem= MOdulator-DEModulator,調(diào)制解調(diào)器
P-P(Plug and Play)即插即用
UPS(Uninterruptable Power Supply)不間斷電源
BIOS(Basic-input-Output
System)基本輸入輸出系統(tǒng)
CMOS(Complementary Metal-Oxide-Semiconductor)互補(bǔ)金屬氧化物半導(dǎo)體
setup安裝
uninstall卸載
wizzard向?qū)?/p>
OS(Operation Systrem)操作系統(tǒng)
OA(Office AutoMation)辦公自動(dòng)化
exit退出
edit編輯
copy復(fù)制
cut剪切
paste粘貼
刪除
select選擇
find查找
select all全選
replace替換
undo撤消
redo重做
program程序
license許可(證)
back前一步
next下一步
finish結(jié)束
folder文件夾
Destination Folder目的文件夾
user用戶
click點(diǎn)擊
double click雙擊
right click右擊
settings設(shè)置
update更新
release發(fā)布
data數(shù)據(jù)
data base數(shù)據(jù)庫(kù)
DBMS(Data Base Manege
System)數(shù)據(jù)庫(kù)管理系統(tǒng)
view視圖
插入
object對(duì)象
configuration配置
command命令
document文檔
POST(power-on-self-test)電源自檢程序
cursor光標(biāo)
attribute屬性
icon圖標(biāo)
service pack服務(wù)補(bǔ)丁
option pack功能補(bǔ)丁
Demo演示
short cut快捷方式
exception異常
debug調(diào)試
previous前一個(gè)
column行
row列
restart重新啟動(dòng)
text文本
font字體
size大小粗信
scale比例
interface界面
function函數(shù)
access訪問
manual指南
active激活
computer language計(jì)算機(jī)語(yǔ)言
menu菜單
GUI(graphical user
悶乎interfaces )圖形用戶界面
template模版
page setup頁(yè)面設(shè)置
password口令
code密碼
print preview打印預(yù)覽
zoom in放大
zoom out縮小
pan漫游
cruise漫游
full screen全屏
tool bar工具條
status bar狀態(tài)條
ruler標(biāo)尺
table表
paragraph段落
symbol符號(hào)
style風(fēng)格
execute執(zhí)行
graphics圖形
image圖像
Unix用于服務(wù)器的一種操作系統(tǒng)
Mac OS蘋果公司開發(fā)的操作系統(tǒng)
OO(Object-Oriented)面向螞凳悉對(duì)象
virus病毒
file文件
open打開
colse關(guān)閉
new新建
save保存
exit退出
clear清除
default默認(rèn)
LAN局域網(wǎng)
WAN廣域網(wǎng)
Client/Server客戶機(jī)/服務(wù)器
ATM( Asynchronous
Transfer Mode)異步傳輸模式
Windows NT微軟公司的網(wǎng)絡(luò)操作系統(tǒng)
Internet互聯(lián)網(wǎng)
WWW(World Wide Web)萬(wàn)維網(wǎng)
protocol協(xié)議
HTTP超文本傳輸協(xié)議
FTP文件傳輸協(xié)議
Browser瀏覽器
homepage主頁(yè)
Webpage網(wǎng)頁(yè)
website網(wǎng)站
URL在Internet的WWW服務(wù)程序上
用于指定信息位置的表示方法
Online在線
Email電子郵件
ICQ網(wǎng)上尋呼
Firewall防火墻
Gateway網(wǎng)關(guān)
HTML超文本標(biāo)識(shí)語(yǔ)言
hypertext超文本
hyperlink超級(jí)鏈接
IP(Address)互聯(lián)網(wǎng)協(xié)議(地址)
SearchEngine搜索引擎
TCP/IP用于網(wǎng)絡(luò)的一組通訊協(xié)議
Telnet遠(yuǎn)程登錄
IE(Internet Explorer)探索者(微軟公司的網(wǎng)絡(luò)瀏覽器)
Navigator引航者(網(wǎng)景公司的瀏覽器)
multimedia多媒體
ISO國(guó)際標(biāo)準(zhǔn)化組織
ANSI美國(guó)國(guó)家標(biāo)準(zhǔn)協(xié)會(huì)
able 能
activefile 活動(dòng)文件
addwatch 添加監(jiān)視點(diǎn)
allfiles 所有文件
allrightsreserved 所有的權(quán)力保留
altdirlst 切換目錄格式
andfixamuchwiderrangeofdiskproblems 并能夠解決更大范圍內(nèi)的磁盤問題
andotherinFORMation 以及其它的信息
archivefileattribute 歸檔文件屬性
assignto 指定到
autoanswer 自動(dòng)應(yīng)答
autodetect 自動(dòng)檢測(cè)
autoindent 自動(dòng)縮進(jìn)
autosave 自動(dòng)存儲(chǔ)
availableonvolume 該盤剩余空間
badcommand 命令錯(cuò)
badcommandorfilename 命令或文件名錯(cuò)
batchparameters 批處理參數(shù)
binaryfile 二進(jìn)制文件
binaryfiles 二進(jìn)制文件
borlandinternational borland國(guó)際公司
bottommargin 頁(yè)下空白
bydate 按日期
byextension 按擴(kuò)展名
byname 按名稱
bytesfree 字節(jié)空閑
callstack 調(diào)用棧
casesensitive 區(qū)分大小寫
causespromptingtoconfirmyouwanttooverwritean 要求出現(xiàn)確認(rèn)提示,在你想覆蓋一個(gè)
centralpointsoftwareinc central point 軟件股份公司
changedirectory 更換目錄
changedrive 改變驅(qū)動(dòng)器
changename 更改名稱
characterset 字符集
checkingfor 正在檢查
checksadiskanddisplaysastatusreport 檢查磁盤并顯示一個(gè)狀態(tài)報(bào)告
chgdrivepath 改變盤/路徑
china 中國(guó)
chooseoneofthefollowing 從下列中選一項(xiàng)
clearall 全部清除
clearallbreakpoints 清除所有斷點(diǎn)
clearsanattribute 清除屬性
clearscommandhistory 清除命令歷史
clearscreen 清除屏幕
closeall 關(guān)閉所有文件
codegeneration 代碼生成
colorpalette 彩色調(diào)色板
commandline 命令行
commandprompt 命令提示符
compressedfile 壓縮文件
configuresaharddiskforusewithmsdos 配置硬盤,以為 MS-DOS 所用
conventionalmemory 常規(guī)內(nèi)存
copiesdirectoriesandsubdirectorie***ceptemptyones 拷貝目錄和子目錄,空的除外
copiesfileswiththearchiveattributeset 拷貝設(shè)置了歸檔屬性的文件
copiesoneormorefilestoanotherlocation 把文件拷貝或搬移至另一地方
copiesthecontentsofonefloppydisktoanother 把一個(gè)軟盤的’內(nèi)容拷貝到另一個(gè)軟盤上
copydiskette 復(fù)制磁盤
copymovecompfindrenamevervieweditattribwordpprintlist C拷貝M移動(dòng) O比 F搜索R改名 D刪除 V版本 E瀏覽A屬性 W寫字 P打印 L列表
copyrightc 版權(quán)(c
createdospartitionorlogicaldosdrive 創(chuàng)建DOS分區(qū)或邏輯DOS驅(qū)動(dòng)器
createextendeddospartition 創(chuàng)建擴(kuò)展DOS分區(qū)
createlogicaldosdrivesintheextendeddospartition 在擴(kuò)展DOS分區(qū)中創(chuàng)建邏輯DOS驅(qū)動(dòng)器
createprimarydospartition 創(chuàng)建DOS主分區(qū)
createsadirectory 創(chuàng)建一個(gè)目錄
createschangesorsthevolumelabelofadisk 創(chuàng)建,改變或刪除磁盤的卷標(biāo)
currentfile 當(dāng)前文件
currentfixeddiskdrive 當(dāng)前硬盤驅(qū)動(dòng)器
currentsettings 當(dāng)前設(shè)置
currenttime 當(dāng)前時(shí)間
cursorposition 光標(biāo)位置
defrag 整理碎片
dele 刪去
partitionorlogicaldosdrive 刪除分區(qū)或邏輯DOS驅(qū)動(dòng)器
sadirectoryandallthesubdirectoriesandfilesinit 刪除一個(gè)目錄和所有的子目錄及其中的所有文件
deltree 刪除樹
devicedriver 設(shè)備驅(qū)動(dòng)程序
dialogbox 對(duì)話欄
directionkeys 方向鍵
directly 直接地
directorylistargument 目錄顯示變量
directoryof 目錄清單
directorystructure 目錄結(jié)構(gòu)
diskaccess 磁盤存取
diskcopy 磁盤拷貝
diskservicescopycomparefindrenameverifyvieweditmaplocateinitialize 磁盤服務(wù)功能: C拷貝 O比較 F搜索R改卷名V校驗(yàn) 瀏覽E編緝M圖 L找文件 N格式化
diskspace 磁盤空間
displayfile 顯示文件
displayoptions 顯示選項(xiàng)
displaypartitioninFORMation 顯示分區(qū)信息
displaysfilesinspecifieddirectoryandallsubdirectories 顯示指定目錄和所有目錄下的文件
displaysfileswithspecifiedattributes 顯示指定屬性的文件
displaysorchangesfileattributes 顯示或改變文件屬性
displaysorsetsthedate 顯示或設(shè)備日期
displayssetupscreensinmonochromeinsteadofcolor 以單色而非彩色顯示安裝屏信息
displaystheamountofusedandfreememoryinyoursystem 顯示系統(tǒng)中已用和未用的內(nèi)存數(shù)量
displaysthefullpathandnameofeveryfileonthedisk 顯示磁盤上所有文件的完整路徑和名稱
displaysthenameoforchangesthecurrentdirectory 顯示或改變當(dāng)前目錄
doctor 醫(yī)生
doesn 不
doesntchangetheattribute 不要改變屬性
dosshell DOS 外殼
doubleclick 雙擊
doyouwanttodisplaythelogicaldriveinFORMationyn 你想顯示邏輯驅(qū)動(dòng)器信息嗎(y/n)?
driveletter 驅(qū)動(dòng)器名
editmenu 編輯選單
ememory ems內(nèi)存
endoffile 文件尾
endofline 行尾
enterchoice 輸入選擇
entiredisk 轉(zhuǎn)換磁盤
environmentvariable 環(huán)境變量
esc esc
everyfileandsubdirectory 所有的文件和子目錄
existingdestinationfile 已存在的目錄文件時(shí)
expandedmemory 擴(kuò)充內(nèi)存
expandtabs 擴(kuò)充標(biāo)簽
explicitly 明確地
extendedmemory 擴(kuò)展內(nèi)存
fastest 最快的
fatfilesystem fat 文件系統(tǒng)
fdiskoptions fdisk選項(xiàng)
fileattributes 文件屬性
fileFORMat 文件格式
filefunctions 文件功能
fileselection 文件選擇
fileselectionargument 文件選擇變?cè)?/p>
filesin 文件在
filesinsubdir 子目錄中文件
fileslisted 列出文件
filespec 文件說(shuō)明
filespecification 文件標(biāo)識(shí)
filesselected 選中文件
findfile 文件查尋
fixeddisk 硬盤
fixeddisksetupprogram 硬盤安裝程序
fixeserrorsonthedisk 解決磁盤錯(cuò)誤
floppydisk 軟盤
FORMatdiskette 格式化磁盤
FORMatsadiskforusewithmsdos 格式化用于MS-DOS的磁盤
FORMfeed 進(jìn)紙
freememory 閑置內(nèi)存
fullscreen 全屏幕
functionprocedure 函數(shù)過程
graphical 圖解的
graphicslibrary 圖形庫(kù)
groupdirectoriesfirst 先顯示目錄組
hangup 掛斷
harddisk 硬盤
hardwaredetection 硬件檢測(cè)
haeen 已經(jīng)
helpfile 幫助文件
helpindex 幫助索引
helpinFORMation 幫助信息
helppath 幫助路徑
helpscreen 幫助屏
helptext 幫助說(shuō)明
helptopics 幫助主題
helpwindow 幫助窗口
hiddenfile 隱含文件
hiddenfileattribute 隱含文件屬性
hiddenfiles 隱含文件
howto 操作方式
ignorecase 忽略大小寫
inbothconventionalanduppermemory 在常規(guī)和上位內(nèi)存
incorrectdos 不正確的DOS
incorrectdosversion DOS 版本不正確
indicatesabinaryfile 表示是一個(gè)二進(jìn)制文件
indicatesanasciitextfile 表示是一個(gè)ascii文本文件
mode 插入方式
insteadofusingchkdsktryusingscandisk 請(qǐng)用scandisk,不要用chkdsk
inuse 在使用
invaliddirectory 無(wú)效的目錄
is 是
kbytes 千字節(jié)
keyboardtype 鍵盤類型
labeldisk 標(biāo)注磁盤
laptop 膝上
largestexecutableprogram 更大可執(zhí)行程序
largestmemoryblockavailable 更大內(nèi)存塊可用
lefthanded 左手習(xí)慣
leftmargin 左邊界
linenumber 行號(hào)
linenumbers 行號(hào)
linespacing 行間距
listbyfilesinsortedorder 按指定順序顯示文件
listfile 列表文件
listof 清單
locatefile 文件定位
lookat 查看
lookup 查找
macroname 宏名字
makedirectory 創(chuàng)建目錄
memoryinfo 內(nèi)存信息
memorymodel 內(nèi)存模式
menubar 菜單條
menucommand 菜單命令
menus 菜單
messagewindow 信息窗口
microsoft 微軟
microsoftantivirus 微軟反病毒軟件
microsoftcorporation 微軟公司
mini 小的
modemsetup 調(diào)制解調(diào)器安裝
modulename 模塊名
monitormode 監(jiān)控狀態(tài)
monochromemonitor 單色監(jiān)視器
moveto 移至
multi 多
newdata 新建數(shù)據(jù)
newer 更新的
newfile 新文件
newname 新名稱
newwindow 新建窗口
norton norton
nostack 棧未定義
noteusedeltreecautiously 注意:小心使用deltree
onlinehelp 聯(lián)機(jī)求助
optionally 可選擇地
or 或
pageframe 頁(yè)面
pagelength 頁(yè)長(zhǎng)
pausesaftereachscreenfulofinFORMation 在顯示每屏信息后暫停一下
pctools pc工具
postscript 附言
prefixmeaningnot 前綴意即”不
prefixtoreverseorder 反向顯示的前綴
presetswitcheyprefixinganyswitchwithhyphenforexamplew 用前綴和放在短橫線-后的開關(guān)(例如/-w)預(yù)置開關(guān)
pressakeytoresume 按一鍵繼續(xù)
pressanykeyforfilefunctions 敲任意鍵執(zhí)行文件功能
pressentertokeepthesamedate 敲回車以保持相同的日期
pressentertokeepthesametime 敲回車以保持相同的時(shí)間
pressesctocontinue 敲esc繼續(xù)
pressesctoexit 敲鍵退出
pressesctoexitfdisk 敲esc退出fdisk
pressesctoreturntofdiskoptions 敲esc返回fdisk選項(xiàng)
previously 在以前
printall 全部打印
printdevice 打印設(shè)備
printerport 打印機(jī)端口
processesfilesinalldirectoriesinthespecifiedpath 在指定的路徑下處理所有目錄下的文件
programfile 程序文件
programmingenvironment 程序設(shè)計(jì)環(huán)境
promptsyoubeforecreatingeachdestinationfile 在創(chuàng)建每個(gè)目標(biāo)文件時(shí)提醒你
promptsyoutopressakeybeforecopying 在拷貝前提示你敲一下鍵
pulldown 下拉
pulldownmenus 下拉式選單
quickFORMat 快速格式化
quickview 快速查看
本文引用自
readonlyfile 只讀文件
readonlyfileattribute 只讀文件屬性
readonlyfiles 只讀文件
readonlymode 只讀方式
redial 重?fù)?/p>
repeatlastfind 重復(fù)上次查找
reportfi
關(guān)于code first 創(chuàng)建數(shù)據(jù)庫(kù)的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗(yàn)。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊(cè)、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
網(wǎng)頁(yè)名稱:利用CodeFirst輕松創(chuàng)建個(gè)性化數(shù)據(jù)庫(kù)(codefirst創(chuàng)建數(shù)據(jù)庫(kù))
當(dāng)前鏈接:http://www.dlmjj.cn/article/djhohpe.html


咨詢
建站咨詢
