日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Entity Framework入门教程:创建实体数据模型

發(fā)布時(shí)間:2023/12/20 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Entity Framework入门教程:创建实体数据模型 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

下圖為一個(gè)已經(jīng)創(chuàng)建好的數(shù)據(jù)庫(kù)表關(guān)系

實(shí)體數(shù)據(jù)模型的創(chuàng)建過(guò)程

在Visual Studio項(xiàng)目中,右鍵程序集菜單,選擇【添加】-》【新建項(xiàng)】,在【添加新項(xiàng)窗口】中選擇【ADO.NET實(shí)體數(shù)據(jù)模型】,如下圖

在【實(shí)體數(shù)據(jù)模型向?qū)А看翱谥羞x擇【來(lái)自數(shù)據(jù)庫(kù)的EF設(shè)計(jì)器】

然后選擇數(shù)據(jù)庫(kù)連接,如果沒(méi)有的話,可以點(diǎn)擊新建一個(gè)連接

然后選擇數(shù)據(jù)庫(kù)對(duì)象

現(xiàn)在a School.edmx文件被添加到了項(xiàng)目中,在這個(gè)文件中包含了school_schema數(shù)據(jù)庫(kù)中表的實(shí)體類(lèi)

創(chuàng)建實(shí)體數(shù)據(jù)模型時(shí)自動(dòng)生成的配置文件

<?xml version="1.0" encoding="utf-8"?> <configuration><configSections><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /></configSections><entityFramework><defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"><parameters><parameter value="mssqllocaldb" /></parameters></defaultConnectionFactory><providers><provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /><provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider></providers></entityFramework> <connectionStrings><add name="SchoolSchemaEntities" connectionString="metadata=res://*/School.csdl|res://*/School.ssdl|res://*/School.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=127.0.0.1;user id=root;password=root;persistsecurityinfo=True;database=school_schema&quot;" providerName="System.Data.EntityClient" /></connectionStrings></configuration>

因?yàn)槲沂褂玫氖荕ySQL,所以你要進(jìn)行上面的操作的話需要安裝一下組件,如果是MSSQL的話,請(qǐng)自行忽略,操作過(guò)程都是一樣的

  • 【MySQL for Visual Studio】
  • 【Connector/Net】

如果遇到如下圖中的錯(cuò)誤,你可以手動(dòng)添加MySql.Data.Entity.EF6.dll(位于MySQL Connector Net的安裝目錄中)

然后添加配置文件,然后再重新進(jìn)行以上操作。

<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>

DbContext

在創(chuàng)建實(shí)體數(shù)據(jù)模型的過(guò)程中,VS為我們生成了一個(gè)SchoolSchemaEntities類(lèi)(該名稱(chēng)是在實(shí)體數(shù)據(jù)模型向?qū)Т翱跀?shù)據(jù)庫(kù)連接設(shè)置的時(shí)候設(shè)定的),它繼承自DbContext(數(shù)據(jù)庫(kù)上下文)。DbContext是 Entity Framework中很重要的一部分,它是實(shí)體類(lèi)和數(shù)據(jù)庫(kù)之前的一道橋梁。

數(shù)據(jù)庫(kù)上下文的功能:

  • EntitySet: 數(shù)據(jù)庫(kù)表的實(shí)體映射集合,形如是DbSet。
  • Querying:將 LINQ-to-Entities查詢(xún)轉(zhuǎn)換為SQL查詢(xún)發(fā)送給數(shù)據(jù)庫(kù)。
  • Change Tracking: 跟蹤從數(shù)據(jù)庫(kù)查詢(xún)的實(shí)體中發(fā)生的更改。
  • Persisting Data:根據(jù)實(shí)體狀態(tài)對(duì)數(shù)據(jù)庫(kù)執(zhí)行插入、更新和刪除操作。
  • Caching: DbContext在默認(rèn)情況下進(jìn)行第一級(jí)緩存。它存儲(chǔ)在上下文類(lèi)的生命周期中檢索的實(shí)體。
  • Manage Relationship: DbContext還可以通過(guò)CSDL、MSL和SSDL在數(shù)據(jù)庫(kù)先行或模型先行方法中管理關(guān)系,或者在代碼優(yōu)先的方法中使用。
  • Object Materialization:DbContext將原始表數(shù)據(jù)轉(zhuǎn)換為實(shí)體對(duì)象。

實(shí)例化DbContext

using (var db = new SchoolSchemaEntities()) {//數(shù)據(jù)操作... }

將DbContext轉(zhuǎn)換成ObjectContext

using (var db = new SchoolSchemaEntities()) {var objectContext = (db as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext; }

原文:
http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx
http://www.entityframeworktutorial.net/EntityFramework4.3/dbcontext-vs-objectcontext.aspx

轉(zhuǎn)載于:https://www.cnblogs.com/yangsofter/p/create-dbcontext.html

總結(jié)

以上是生活随笔為你收集整理的Entity Framework入门教程:创建实体数据模型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。