EntitySpaces2009中的关系
来源:互联网 发布:java和嵌入式 编辑:程序博客网 时间:2024/06/05 19:39
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();
操作完成后,对应的关系会更新到关系表中。
- EntitySpaces2009中的关系
- EntitySpaces2009支持事务
- EntitySpaces2009的开发文档地址
- EntitySpaces2009中连接Access的连接设置
- 关系数据库中的“键”
- java中的继承关系
- 类图中的关系
- UML中的关系定义
- Rails 中的映射关系:
- UML中的关系定义
- Hibernate中的关联关系
- 汇编中的堆栈关系
- 继承中的成员关系
- 类图中的关系
- JBPM中的表关系
- Hibernate中的一对一关系
- 关系代数中的除法
- dataset中的数据关系
- EntitySpaces2009支持事务
- hibernate 数据保存操作方法的原理对比
- EntitySpaces2009中连接Access的连接设置
- JAVA图片水印效果代码
- 散列表(哈希表)-- 计算机科学里的一个伟大发明
- EntitySpaces2009中的关系
- Sync Moto-A1200 with GMAIL contacts by SyncML
- 兽药价值营销对抗同质化
- Linux /proc/cpuinfo flags
- JSP文件下载
- PKU2299-求逆序数的题-实现(归并排序和树状数组)
- 如何得到其他程序焦点变化和启动的通知
- PostgreSQL RPM 安装笔记
- Android开发学习日记 -- 1.安装与HelloWorld