Entity Framework 6 Recipes 2nd Edition(13-2)译 - 用实体键获取一个单独的实体
問題
不管你用DBFirst,ModelFirst或是CodeFirst的方式,你想用實體鍵獲取一個單獨的實體.在本例中,我們用CodeFirst的方式.
解決方案
假設你有一個模型表示一個Painting(繪畫)類型的實體,如Figure 13-2所示:
Figure 13-2. The Painting entity type in our model
?
在代碼In Listing 13-2,我們創建實體類Painting.
? public class Painting
??? {
??????? public string AccessionNumber { get; set; }
??????? public string Name { get; set; }
??????? public string Artist { get; set; }
??????? public decimal LastSalePrice { get; set; }
}
接下來,在代碼Listing 13-3,我們創建DbContext對象(我們用CodeFirst方式時,EF的門戶)
Listing 13-3. DbContext Object
? public class Recipe2Context : DbContext
??? {
??????? public Recipe2Context()
??????????? : base("Recipe2ConnectionString")
??????? {
??????????? // Disable Entity Framework Model Compatibility
??????????? Database.SetInitializer<Recipe2Context>(null);
??????? }
??????? protected override void OnModelCreating(DbModelBuilder modelBuilder)
??????? {
??????????? // map AccessionNumber as primary key to table
??????????? modelBuilder.Entity<Painting>().HasKey(x => x.AccessionNumber);
??????????? modelBuilder.Entity<Painting>().ToTable("Chapter13.Painting");
??????? }
??????? public DbSet<Painting> Paintings { get; set; }
}
接下來在項目里添加App.config,并把Listing 13-4代碼添加到ConnectionStrings節下,
Listing 13-4. Connection String
<connectionStrings>
<add name="Recipe2ConnectionString"
connectionString="Data Source=.;
Initial Catalog=EFRecipes;
Integrated Security=True;
MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
在代碼Listing 13-5,我們裝載數據,演示兩種獲取實體的方法:LinQ查詢和Find()方法.
Listing 13-5. Retrieving an Entity with the Find() Method
?? private static void RunExample()
??????? {
??????????? using (var context = new Recipe2Context())
??????????? {
??????????????? context.Paintings.Add(new Painting
??????????????? {
??????????????????? AccessionNumber = "PN001",
??????????????????? Name = "Sunflowers",
??????????????????? Artist = "Rosemary Golden",
????????????????? ??LastSalePrice = 1250M
??????????????? });
??????????? }
??????????? using (var context = new Recipe2Context())
??????????? {
??????????????? // LINQ 查詢總是會從數據庫獲取實體,即使實體已經存在于上下文中
??????????????? var paintingFromDatabase =
??????????????? context.Paintings.FirstOrDefault(x => x.AccessionNumber == "PN001");
?
??????????????? // Find() 方法會從上下文中獲取實體,如果不存在再從數據庫獲取
??????????????? var paintingFromContext = context.Paintings.Find("PN001");
??????????? }
??????????? Console.WriteLine("Press <enter> to continue...");
??????????? Console.ReadLine();
??????? }
?
它是如何工作的
當用LinQ查詢時,它都會從數據庫中獲取所需的數據,即使這些數據已經被裝載進了內存中的上下文(Context)對象里,當這個查詢結束,如果實體尚不存在于上下文,那么則會被加入上下文,并且被跟蹤.默認情況下,如果實體對象已經存在于上下文,它不會被從數據庫新獲取的數據重寫.
???????? 然而,封裝了實體對象的DbSet對象,暴露一個Find()方法,該方法需要提供一個主鍵參數.如組合主鍵,就需要給它傳遞一個數組.Find()方法是非常有效率的,它先會在上下文中查找目標對象,如果找到,它就直接從上下文中返回,如果找不到,它自動查詢數據庫,如果還是找不到,就返回null.另外,Find()方法可以返回一個已經被添加(狀態為Added)但未保存到數據庫的實體.而且Find()方法可以在三種方式中使用:DBFirst,ModelFirst,CodeFirst.
???????? 在Listing 13-5,我們先調用LinQ查詢來獲取一個Painting.圖 Figure 13-3顯示了產生的SQL查詢
Figure 13-3. SQL Query returning our painting
接著的下行代碼,我們用Find()方法來獲取相同的實體,我們給它傳遞了一個主鍵參數(“PN001”),它首先在上下文對象中查找主鍵為PN001的實體,找到之后,返回該實體的一個引用,避免了對數據庫查詢.如圖Figure 13-4所示,數據庫中并沒有SQL語句產生
Figure 13-4. The Find() method locates the object in memory, not generating a database query
???????? 查看 Find()方法的更多信息,請查看 Recipe 5-3.
總結
以上是生活随笔為你收集整理的Entity Framework 6 Recipes 2nd Edition(13-2)译 - 用实体键获取一个单独的实体的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 梦到满屋的蛆是怎么回事
 - 下一篇: 梦到被一群猫狗围攻怎么回事