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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

初识Entity Framework CodeFirst(3)

發(fā)布時間:2025/3/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 初识Entity Framework CodeFirst(3) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前兩回合,我們討論學(xué)習(xí)了如何采用Entity Framework在沒有數(shù)據(jù)庫的情況下自己寫一些實(shí)體類,然后通過CodeFirst反向生成對應(yīng)的數(shù)據(jù)庫。通過CodeFirst,我們擺脫了edmx文件,沒有了繁瑣的Xml關(guān)系映射,使代碼變得無比的清晰,修改起來也變得更加容易。

在前兩篇文章中,我們的數(shù)據(jù)庫都是通過反向生成的,也就是說,屬于一個New Database(新數(shù)據(jù)庫),那么,對于一個Exist Database(已存在的數(shù)據(jù)庫),我們又應(yīng)該怎么辦呢?本節(jié)文章,我們將對?Code First to an Existing Database 做出討論學(xué)習(xí)。

本回合我們將討論:

1、介紹使用“Entity Framework Power Tools“ 工具

2、EF CodeFirst to Existing Database 的快速入門

3、了解”Entity Framework Power Tools“ 工具為我們做了哪些事

案例代碼可以點(diǎn)擊這里下載


1、Entity Framework Power Tools 工具

在看完本節(jié)的導(dǎo)讀時,也許你已經(jīng)有了自己的一點(diǎn)想法:“按照我的數(shù)據(jù)庫結(jié)構(gòu),自己手寫出一些對應(yīng)的實(shí)體Model,然后再寫一個繼承自DbContext的上下文……”。沒錯,恭喜你,你的想法理論上確實(shí)是可行的,至少在表不太多的情況下是可行的。但是,如果這個數(shù)據(jù)庫的表有很多呢,數(shù)目已經(jīng)達(dá)到了上百個或者更多呢?明顯的,我們?nèi)绻胱约菏謱憣?shí)體和上下文,那無疑是一件巨大的工程。

既要懂得原理,也要懂得效率;我們要做某一件事,必須先要懂得它的原理,為什么要這么做,知其然知其所以然,所謂“原理先行”嘛。懂了了原理之后,我們需要提高效率,這在當(dāng)今這個效率就是金錢的時代中尤為重要。

不扯開話題,下面我為大家介紹一款工具,它的名字就是:“Entity Framework Power Tools”,它的可以實(shí)現(xiàn),對數(shù)據(jù)庫中已存在的表自動的生成相應(yīng)的實(shí)體和上下文。這款工具的下載地址各位讀者可以點(diǎn)擊這里獲得最新版的下載。

各位讀者下載下來之后,雙擊打開安裝,然后再重啟Visual Studio實(shí)例,就可以使用啦。

?

2、快速入門(快速案例)

我們新建一個解決方案                          

再看看我們的數(shù)據(jù)庫,這里我采用上回合反向生成的數(shù)據(jù)庫(由于VS自帶的數(shù)據(jù)庫管理器用起來速度太慢,我額外裝了一個“SQL Server Management Studio”,數(shù)據(jù)庫實(shí)例還是采用原先的輕量級數(shù)據(jù)庫LocalDB,區(qū)別的僅僅是管理工具不同,數(shù)據(jù)庫實(shí)例還是一樣,各位讀者不必感覺疑惑)

右鍵點(diǎn)擊解決方案,我們發(fā)現(xiàn)了一些新東西

這就是我們剛才安裝的EF工具,順著點(diǎn)擊進(jìn)去,我們會彈出一個SQL Server的連接窗口,填寫好相應(yīng)的連接信息,點(diǎn)擊確定

稍等一會兒,系統(tǒng)會自動的連接數(shù)據(jù)庫,然后遍歷所有的表,并根據(jù)表結(jié)構(gòu)生成一些代碼。

我們可以看到,解決方案中多了一個“Model”文件夾,里面包含有上下文、一些Model、“Mapping”文件夾以及其下的Map文件(映射文件)

我們先不解釋里面的東西,先在Program中寫調(diào)用代碼,解釋留到第三節(jié)再解析。

我們在Program中寫如下調(diào)用代碼:

class Program{static void Main(string[] args){using (var db = new CodeFirst_2013_3_23BlogContextContext()){Console.Write("Enter a name for a new blog:");var name = Console.ReadLine();var blog = new Blog { BlogName = name };db.Blogs.Add(blog);db.SaveChanges();var result = from b in db.Blogsselect b;foreach (var item in result){Console.WriteLine(item.BlogName);}}Console.ReadKey();}}

F5,調(diào)試:

可以正常執(zhí)行(上面那個小蝶驚鴻是上回合CodeFirst操作中遺留下來的數(shù)據(jù))

?

3、EF工具幫我們生成了什么

上一節(jié)我們是一個快速入門,這一節(jié)我們看看EF Tool幫我們生成了些什么代碼。

先看一下App.config

App.config <?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><connectionStrings><add name="CodeFirst_2013_3_23BlogContextContext" connectionString="Data Source=(localdb)\mydb;Initial Catalog=CodeFirst_2013_3_23.BlogContext;Integrated Security=True;MultipleActiveResultSets=True"providerName="System.Data.SqlClient" /></connectionStrings><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup><entityFramework><defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"><parameters><parameter value="v11.0" /></parameters></defaultConnectionFactory></entityFramework> </configuration>

系統(tǒng)幫我們自動的填寫配置信息,值得注意的是“connectionStrings”節(jié)點(diǎn),這里配置了EF的連接字串,在初始化上下文的對象時,上下文的構(gòu)造函數(shù)會默認(rèn)的傳入這里的連接字串。

<connectionStrings><add name="CodeFirst_2013_3_23BlogContextContext" connectionString="Data Source=(localdb)\mydb;Initial Catalog=CodeFirst_2013_3_23.BlogContext;Integrated Security=True;MultipleActiveResultSets=True"providerName="System.Data.SqlClient" /></connectionStrings>

進(jìn)入“Model”文件夾

這里生成了一些實(shí)體類文件:

Post實(shí)體:

Post public partial class Post{public int PostID { get; set; }public string Title { get; set; }public string Content { get; set; }public int BlogID { get; set; }public virtual Blog Blog { get; set; }}

Blog實(shí)體:

Blog public partial class Blog{public Blog(){this.Posts = new List<Post>();}public int BlogID { get; set; }public string BlogName { get; set; }public string Url { get; set; }public virtual ICollection<Post> Posts { get; set; }}

Type實(shí)體:

Type public partial class Type{public int TypeID { get; set; }public string TypeName { get; set; }}

User實(shí)體:

User public partial class User{public string UserName { get; set; }public string Display_Name { get; set; }}

還有上下文類CodeFirst_2013_3_23BlogContextContext:

CodeFirst_2013_3_23BlogContextContext public partial class CodeFirst_2013_3_23BlogContextContext : DbContext{static CodeFirst_2013_3_23BlogContextContext(){Database.SetInitializer<CodeFirst_2013_3_23BlogContextContext>(null);}public CodeFirst_2013_3_23BlogContextContext(): base("Name=CodeFirst_2013_3_23BlogContextContext"){}public DbSet<Blog> Blogs { get; set; }public DbSet<Post> Posts { get; set; }public DbSet<Type> Types { get; set; }public DbSet<User> Users { get; set; }protected override void OnModelCreating(DbModelBuilder modelBuilder){modelBuilder.Configurations.Add(new BlogMap());modelBuilder.Configurations.Add(new PostMap());modelBuilder.Configurations.Add(new TypeMap());modelBuilder.Configurations.Add(new UserMap());}}

該上下文構(gòu)造方法調(diào)用了父類DbContext的構(gòu)造方法,傳入配置文件中的數(shù)據(jù)庫連接字串來連接需要的數(shù)據(jù)庫。

(反編譯DbContext得知其構(gòu)造方法實(shí)際上是重載的方法,我們可以傳入多種的參數(shù)形式,這里我們不做過多的介紹)

最后,我們進(jìn)入“Mapping”文件夾

里面包含的都是實(shí)體與數(shù)據(jù)庫,屬性與字段映射的關(guān)系文件,作用跟使用edmx文件時,那繁瑣的XML映射作用是一樣的,不過,采用CodeFirst方式生成的關(guān)系映射,代碼都是C#語言的,并且看起來相當(dāng)清晰,以后重構(gòu)起來也比edmx的XML方便得多。


至此,本回合的CodeFirst to Existing Database已經(jīng)討論講解完畢,個人能力有限,可能文中會有錯漏的地方,歡迎各位朋友指正以及提出建議。

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/xiaodiejinghong/archive/2013/03/25/2981232.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的初识Entity Framework CodeFirst(3)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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