信息化基础建设 ORM 常见错误

ORM 常见错误

1. 根对象引用错误

// order collection to fetch

CustomerEntity myCustomer = new CustomerEntity(11091);

// prepare prefetch (customer -> salesOrder)

IPrefetchPath2 myCustomerPath = new PrefetchPath2((int)EntityType.CustomerEntity);

myCustomerPath.Add(CustomerEntity.PrefetchPathSalesOrders);

myCustomerPath.Add(SalesOrderEntity.PrefetchPathSalesOrderDetails);

 

2. 在同级别的层次中使用SubPath多次

// order collection to fetch

CustomerEntity myCustomer = new CustomerEntity(11091);

// prepare prefetch (customer -> salesOrder)

IPrefetchPath2 myCustomerPath = new PrefetchPath2((int)EntityType.CustomerEntity);

myCustomerPath.Add(CustomerEntity.PrefetchPathSalesOrders)

.SubPath.Add(SalesOrderEntity.PrefetchPathSalesOrderDetails)

.SubPath.Add(SalesOrderEntity.PrefetchPathShipMethod);

 

3. PrefetchPath应该是在根对象中使用,非子对象

// order collection to fetch

EntityCollection<AddressEntity> addresses = new EntityCollection<AddressEntity>();

// just wanna the addresses from Ottawa

IPredicateExpression orderFilter = new PredicateExpression(AddressFields.City == "Ottawa");
IRelationCollection orderRelations = new RelationCollection();

orderRelations.Add(SalesOrderEntity.Relations.AddressEntityUsingShipToAddressId);

在子对象中使用filter的例子

// for those addresses, give their orders

IPrefetchPath2 myAddressesPath = new PrefetchPath2((int)EntityType.AddressEntity);

myAddressesPath.Add(AddressEntity.PrefetchPathShippedSalesOrders, 0, orderFilter, orderRelations);

// fetch

adapter.FetchEntityCollection(addresses, null, myAddressesPath);

 

4. 在root entity上应用排序,并不会给子对象应用排序

// addresses to fetch

EntityCollection<AddressEntity> addresses = new EntityCollection<AddressEntity>();

// want the orders sorted by totalDue(cheapeast first)

SortExpression sorter = new SortExpression(SalesOrderFields.TotalDue | SortOperator.Descending);

// use relations to be able to sort

IRelationPredicateBucket bucket = new RelationPredicateBucket();

bucket.Relations.Add(AddressEntity.Relations.SalesOrderEntityUsingShipToAddressId);

// for those addresses, give their orders

IPrefetchPath2 myAddressPath = new PrefetchPath2((int)EntityType.AddressEntity);

myAddressPath.Add(AddressEntity.PrefetchPathShippedSalesOrders);

// fetch

adapter.FetchEntityCollection(addresses, bucket, 10, sorter, myAddressPath);

在子对象中排序的方法

// addresses to fetch

EntityCollection<AddressEntity> addresses = new EntityCollection<AddressEntity>();

// want the orders sorted by totalDue(cheapeast first)

SortExpression sorter = new SortExpression(

SalesOrderFields.TotalDue | SortOperator.Descending);

// for those addresses, give me their orders (sorted)

IPrefetchPath2 myAddressPath = new PrefetchPath2((int)EntityType.AddressEntity);

myAddressPath.Add(AddressEntity.PrefetchPathShippedSalesOrders, 0, null, null, sorter);

// fetch

adapter.FetchEntityCollection(addresses, null, 10, null, myAddressPath);

 

5. 一个root entity有多个子entity collection时,需要分别设置prefetchpath

// the product to fetch

ProductEntity myProduct = new ProductEntity(506);

// prepare the path (product -> productDocuemnt -> document)

IPrefetchPath2 myProductPath = new PrefetchPath2((int)EntityType.ProductEntity);

myProductPath.Add(ProductEntity.PrefetchPathProductDocuments)

.SubPath.Add(ProductDocumentEntity.PrefetchPathDocument);

// fetch

adatper.FetchEntity(myProduct, myProductPath);

正确的代码

// the product to fetch

ProductEntity myProduct = new ProductEntity(506);

// prepare the path (product -> productDocuemnt -> document,

// product -> document)

IPrefetchPath2 myProductPath = new PrefetchPath2((int)EntityType.ProductEntity);

myProductPath.Add(ProductEntity.PrefetchPathProductDocuments)

  .SubPath.Add(ProductDocumentEntity.PrefetchPathDocument);

myProductPath.Add(ProductEntity.PrefetchPathDocuments);

// fetch

adatper.FetchEntity(myProduct, myProductPath);

 

6. The X -> Y -> X graph

// order to fetch

SalesOrderEntity myOrder = new SalesOrderEntity(43659);

// prepare path (salesOrder -> customer -> salesOrder)

IPrefetchPath2 myOrderPath = new PrefetchPath2((int) EntityType.SalesOrderEntity);

myOrderPath.Add(SalesOrderEntity.PrefetchPathCustomer)

.SubPath.Add(CustomerEntity.PrefetchPathSalesOrders);

// fetch

adaptter.FetchEntity(myOrder, myOrderPath);

正确的写法

// order to fetch

SalesOrderEntity myOrder = new SalesOrderEntity(43659);

// prepare path (salesOrder -> customer -> salesOrder)

IPrefetchPath2 myOrderPath = new PrefetchPath2((int)EntityType.SalesOrderEntity);

myOrderPath.Add(SalesOrderEntity.PrefetchPathCustomer)

    .SubPath.Add(CustomerEntity.PrefetchPathSalesOrders);

// fetch

Context myContex = new Context();

adaptter.FetchEntity(myOrder, myOrderPath, myContex);

 

7. 合并查询语句

SELECT [Northwind].[dbo].[Customers].[ContactName] ,

ISNULL(( SELECT COUNT([Northwind].[dbo].[Orders].[OrderID]) AS [OrderId]

FROM ( [Northwind].[dbo].[Orders]

INNER JOIN [Northwind].[dbo].[Customers] ON [Northwind].[dbo].[Customers].[CustomerID] = [Northwind].[dbo].[Orders].[CustomerID]

)

WHERE ( [Northwind].[dbo].[Customers].[CustomerID] = [Northwind].[dbo].[Orders].[CustomerID] )

), 0.) AS [Total]

FROM [Northwind].[dbo].[Customers]

这两个查询可以合并为一个查询

SELECT * FROM [Customers] WHERE dbo.Customers.CustomerID='BERGS'

SELECT COUNT(1) FROM dbo.Orders WHERE CustomerID='BERGS' --18

--合并为一个查询的结果

SELECT [Northwind].[dbo].[Customers].[ContactName] ,

ISNULL(( SELECT COUNT([Northwind].[dbo].[Orders].[OrderID]) AS [OrderId]

FROM [Northwind].[dbo].[Orders]

WHERE ( [Northwind].[dbo].[Orders].[CustomerID] = [Northwind].[dbo].[Customers].[CustomerID] )

), 0.) AS [Total]

FROM [Northwind].[dbo].[Customers]

WHERE dbo.Customers.CustomerID='BERGS'

posted @ 2011-06-13 09:21  信息化建设  阅读(677)  评论(0编辑  收藏  举报