Entity Framework入门教程:创建实体数据模型
下圖為一個已經(jīng)創(chuàng)建好的數(shù)據(jù)庫表關(guān)系
實(shí)體數(shù)據(jù)模型的創(chuàng)建過程
在Visual Studio項(xiàng)目中,右鍵程序集菜單,選擇【添加】-》【新建項(xiàng)】,在【添加新項(xiàng)窗口】中選擇【ADO.NET實(shí)體數(shù)據(jù)模型】,如下圖
在【實(shí)體數(shù)據(jù)模型向?qū)А看翱谥羞x擇【來自數(shù)據(jù)庫的EF設(shè)計器】
然后選擇數(shù)據(jù)庫連接,如果沒有的話,可以點(diǎn)擊新建一個連接
然后選擇數(shù)據(jù)庫對象
現(xiàn)在a School.edmx文件被添加到了項(xiàng)目中,在這個文件中包含了school_schema數(shù)據(jù)庫中表的實(shí)體類
創(chuàng)建實(shí)體數(shù)據(jù)模型時自動生成的配置文件
<?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="server=127.0.0.1;user id=root;password=root;persistsecurityinfo=True;database=school_schema"" providerName="System.Data.EntityClient" /></connectionStrings></configuration>因?yàn)槲沂褂玫氖荕ySQL,所以你要進(jìn)行上面的操作的話需要安裝一下組件,如果是MSSQL的話,請自行忽略,操作過程都是一樣的
- 【MySQL for Visual Studio】
- 【Connector/Net】
如果遇到如下圖中的錯誤,你可以手動添加MySql.Data.Entity.EF6.dll(位于MySQL Connector Net的安裝目錄中)
然后添加配置文件,然后再重新進(jìn)行以上操作。
DbContext
在創(chuàng)建實(shí)體數(shù)據(jù)模型的過程中,VS為我們生成了一個SchoolSchemaEntities類(該名稱是在實(shí)體數(shù)據(jù)模型向?qū)Т翱跀?shù)據(jù)庫連接設(shè)置的時候設(shè)定的),它繼承自DbContext(數(shù)據(jù)庫上下文)。DbContext是 Entity Framework中很重要的一部分,它是實(shí)體類和數(shù)據(jù)庫之前的一道橋梁。
數(shù)據(jù)庫上下文的功能:
- EntitySet: 數(shù)據(jù)庫表的實(shí)體映射集合,形如是DbSet。
- Querying:將 LINQ-to-Entities查詢轉(zhuǎn)換為SQL查詢發(fā)送給數(shù)據(jù)庫。
- Change Tracking: 跟蹤從數(shù)據(jù)庫查詢的實(shí)體中發(fā)生的更改。
- Persisting Data:根據(jù)實(shí)體狀態(tài)對數(shù)據(jù)庫執(zhí)行插入、更新和刪除操作。
- Caching: DbContext在默認(rèn)情況下進(jìn)行第一級緩存。它存儲在上下文類的生命周期中檢索的實(shí)體。
- Manage Relationship: DbContext還可以通過CSDL、MSL和SSDL在數(shù)據(jù)庫先行或模型先行方法中管理關(guān)系,或者在代碼優(yōu)先的方法中使用。
- Object Materialization:DbContext將原始表數(shù)據(jù)轉(zhuǎn)換為實(shí)體對象。
實(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入门教程:创建实体数据模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017.6.26小学期1
- 下一篇: centos7 mysql安装与用户设置