中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ADO.NET Entity FrameWork

發布時間:2020-07-28 02:21:04 來源:網絡 閱讀:367 作者:eryongyan 欄目:編程語言

 

缺點:Entity FrameWork 沒有提供獲取不同層上的對象的方式,無助于創建多層上的解決方案,可以結合WCF實現分層

核心概念:

邏輯層,SSDL store schema definition language描述數據庫表及其關系

概念層,CSDL conceptual schema definition L  描述 實體及關系

映射層 MSL  mapping Specification L  把CSDL實體類型定義映射到SSDL

查詢對象的三種方法

1. Entity SQL

Technorati 標簽: ADO.NET Entity FrameWork

using (var data = new Formula1Entities())

            {

                string country = "Brazil";

                ObjectQuery<Racer> racers = data.CreateQuery<Racer>(

                   "SELECT VALUE it FROM ([Formula1Entities].[Racers]) AS it WHERE it.Country = @Country",

                   new ObjectParameter("Country", country));

                foreach (var r in racers)

                {

                    Console.WriteLine("{0} {1}", r.Firstname, r.Lastname);

                }

            }

2.QueryBuilder

類似于LINQ,但Linq的參數是委托,而QueryBuilder的參數是字符串

可以用Find(),GroupBy(),OrderBy()等方法,

  using (var data = new Formula1Entities())

            {

                string country = "Brazil";

                ObjectQuery<Racer> racers = data.Racers.Where("it.Country = @Country", new ObjectParameter("Country", country));

                Console.WriteLine(racers.CommandText);

                Console.WriteLine(racers.ToTraceString());

            }

3. Linq to Entities

因為實體對象都繼承于ObjectQuery<T>,而該類實現了IQueryable接口

using (var data = new Formula1Entities())

            {

                var racers = from r in data.Racers

                             where r.Wins > 40

                             orderby r.Wins descending

                             select r;

                foreach (Racer r in racers)

                {

                    Console.WriteLine("{0} {1}", r.Firstname, r.Lastname);

                }

            }

增刪改查CRUD的實現

  using (var data = new Formula1Entities())

            {

                var jaime = new Racer

                {

                    Firstname = "Jaime",

                    Lastname = "Alguersuari",

                    Country = "Spain",

                    Starts = 0

                };

                data.Racers.AddObject(jaime);

           //添加

Racer niki1 = data.Racers.Where("it.Country='Austria' && it.Lastname='Berger'").First();

   data.Racers.DeleteObject( niki1 );

//刪除

  Racer niki2 = data.Racers.Where("it.Country='Austria'").OrderBy("it.Wins DESC").First();

niki2.Stars++;//修改

                int changes = 0;

                try

                {

                    changes += data.SaveChanges();

//提交到數據源

                }

   catch (OptimisticConcurrencyException ex)

                {

                    data.Refresh(RefreshMode.ClientWins, ex.StateEntries);

                    changes += data.SaveChanges();

                }

}

//懶惰加載

using (var data = new NorthwindEntities())

            {

                data.ContextOptions.LazyLoadingEnabled = false;

                int a = data.Customers.Count();

                foreach (Customer customer in data.Customers.Include("Orders"))

                {

                    Console.WriteLine("{0}", customer.CompanyName);

                    bool a2 = customer.Orders.IsLoaded;

              //   if (!customer.Orders.IsLoaded)

              //      {

              //          customer.Orders.Load();

                    }

                    foreach (Order order in customer.Orders)

                    {

                        Console.WriteLine("\t{0} {1:d}", order.OrderID, order.OrderDate);

                    }

                }

            }

data.ContextOptions.LazyLoadingEnabled = false;

LazyLoadingEnabled  懶惰加載,意思是在訪問對象時,會自動加載對象的相關屬性   

foreach (Customer customer in data.Customers)

LazyLoadingEnabled  默認為true,若設為false,則需要使用

Include("Orders")) 稱為預先加載

     customer.Orders.Load();(延遲加載

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

凤凰县| 蒲城县| 涟水县| 巴中市| 高尔夫| 芦山县| 平阳县| 林芝县| 连州市| 乌恰县| 江永县| 彩票| 荆门市| 曲周县| 禹城市| 连南| 阳曲县| 铁岭市| 石首市| 老河口市| 青河县| 桦川县| 内乡县| 仙桃市| 独山县| 永顺县| 霍邱县| 福清市| 宁海县| 图木舒克市| 宁乡县| 肇东市| 双江| 新蔡县| 克什克腾旗| 莫力| 驻马店市| 静安区| 治县。| 亚东县| 建阳市|