EntitySpaces2009中的关系

EntitySpaces2009中的关系使用起来还是很方便的,其中包括四种类型:Zero To Many, One To One, Many To One, Many To Many

使用时可以针对不同的关系通过对应的代码模式完成。

 

Zero-To-Many

A Zero To Many uses a collection to represent the nested sub-object. Here we are going to loop through all the OrderDetails for a given Product. This example raises the prices by 10%.

通过主表记录更新附表记录方法:

Products product = new Products();
product.LoadByPrimaryKey(1);

foreach(OrderDetails od in product.OrderDetailsCollectionByProductID)
{
 od.UnitPrice *= 0.10M;
}

product.Save();

通过主表记录添加子表记录:

Now we are going to add an OrderDetails record to this Products data.

 

Products product = new Products();
product.LoadByPrimaryKey(1);

OrderDetails od = product.OrderDetailsCollectionByProductID.AddNew();
od.UnitPrice = 45.23M;
od.Discount = 0.05M;
od.Quantity = 4;
 
// Notice we didn't have to assign the ProductID to our OrderDetails record,
// EntitySpaces does this automatically for you.
product.Save();

 

Many-To-One

Let's start out by looking at a Many To One example using the Northwind Products table which has a CategoryID that points to the product's category. The code below loads a product, then accesses its category sub-object printing the name.  A many to one uses an Entity to represent the nested sub-object.

 

Products product = new Products();
product.LoadByPrimaryKey(1);

// Access Category sub-object
string name = product.UpToCategoriesByCategoryID.CategoryName;
 
// Traditional way to set the Category
product.CategoryID = 42;
 
// Alternate way to set the category
Categories cat = new Categories();
cat.LoadByPrimaryKey(42);

product.CategoryID = cat.CategoryID;
 
product.Save();

 

Many-To-Many

A Many-to-Many like the Zero-to-Many uses a collection to represent the nested sub-object. There are two new methods on a Many-To-Many and they are Associate and Dissociate. EntitySpaces implements the Many-to-Many in such a way that you never actually see the linking table. However, there are EntitySpaces objects for the linking table, they are just hidden away to make your life easier.

In this example we are going to loop through all of the Territories for a given Employee. Behind the scenes there is set of EntitySpaces classes to represent the EmployeeTerritories table however they are not exposed though the programmer can use them at will whenever desired.

 

遍历记录的关联记录:

Employees e = new Employees();
e.LoadByPrimaryKey(1);

foreach(Territories t in e.UpToTerritoriesCollection)
{
 Console.WriteLine(t.TerritoryDescription);
}

建立或删除两记录之间的关系

You can also associate and dissociate Territories as follows.

 

Employees e = new Employees();
e.LoadByPrimaryKey(1);

Territories terr = new Territories();
terr.LoadByPrimaryKey("07960");
 
e.AssociateTerritoriesCollection(terr);
// or
e.DissociateTerritoriesCollection(terr);
 
e.Save();

 

操作完成后,对应的关系会更新到关系表中。

转载于:https://www.cnblogs.com/Rising/archive/2010/01/31/1660367.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值