zoukankan      html  css  js  c++  java
  • 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();

    操作完成后,对应的关系会更新到关系表中。
  • 相关阅读:
    robotframework之配置文件和资源文件
    robotframework-ride创建测试用例过程
    Robot Framework的环境搭建
    Robot Framework的初始化与清除
    robotframework的安装1
    《这么慢,那么美》摘录
    小白摸索记(一)
    学习笔记-express路径问题
    学习笔记-使用cmd命令行创建nodejs项目
    学习笔记
  • 原文地址:https://www.cnblogs.com/Rising/p/1660367.html
Copyright © 2011-2022 走看看