缺點(diǎn):Entity FrameWork 沒有提供獲取不同層上的對象的方式,無助于創(chuàng)建多層上的解決方案,可以結(jié)合WCF實(shí)現(xiàn)分層
專注于為中小企業(yè)提供做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)廣州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
核心概念:
邏輯層,SSDL store schema definition language描述數(shù)據(jù)庫表及其關(guān)系
概念層,CSDL conceptual schema definition L 描述 實(shí)體及關(guān)系
映射層 MSL mapping Specification L 把CSDL實(shí)體類型定義映射到SSDL
查詢對象的三種方法
1. Entity SQL
using (var data = new Formula1Entities())
{
string country = "Brazil";
ObjectQuery
"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的參數(shù)是委托,而QueryBuilder的參數(shù)是字符串
可以用Find(),GroupBy(),OrderBy()等方法,
using (var data = new Formula1Entities())
{
string country = "Brazil";
ObjectQuery
Console.WriteLine(racers.CommandText);
Console.WriteLine(racers.ToTraceString());
}
3. Linq to Entities
因?yàn)閷?shí)體對象都繼承于ObjectQuery
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的實(shí)現(xiàn)
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();
//提交到數(shù)據(jù)源
}
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 懶惰加載,意思是在訪問對象時(shí),會自動加載對象的相關(guān)屬性
foreach (Customer customer in data.Customers)
LazyLoadingEnabled 默認(rèn)為true,若設(shè)為false,則需要使用
Include("Orders")) 稱為預(yù)先加載
customer.Orders.Load();(延遲加載