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();

    操作完成后,对应的关系会更新到关系表中。
  • 相关阅读:
    软件开发(目录规范)
    面向对象编程(UDP协议)
    Django+uwsgi+Nginx
    Django(图书管理系统)
    orm(Manager isn't accessible via %s instances" % cls.__name)报错
    Django(图书管理系统)#转
    docker(mysql-redmine)
    Django(orm)转
    Django(多表查询操作)
    Oracle(ERROR SP2-0642)
  • 原文地址:https://www.cnblogs.com/Rising/p/1660367.html
Copyright © 2011-2022 走看看