This project is read-only.

Let's assume we have two Entity types, "Foo" and "Bar" and we’re looking at the relationship from the perspective of the Foo Entity.

There are, generally, four types of Entity relationships that can exist between Foo and Bar.

  • One to Many
    • One Foo can be related to Many different Bar objects (or one Foo can contain many Bars). An example might be A customer (Foo) might have several Orders (bar)

      [Entity(KeyScheme.Identity)]
      public class Customer
      {
          [Field(IsPrimaryKey = true)]
          public int CustomerID { get; set; }
      
          [Field]
          public string Name { get; set; }
      
          [Reference(typeof(ProductOrder), "CustomerID", ReferenceType=ReferenceType.OneToMany)]
          public ProductOrder[] Orders { get; set; }
      }
      
      [Entity(KeyScheme.Identity)]
      public class ProductOrder
      {
          public ProductOrder()
          {
              // this is required for cascading inserts to work
              OrderID = -1;
          }
      
          [Field(IsPrimaryKey = true)]
          public int OrderID { get; set; }
      
          [Field] // this is the foreign key
          public int CustomerID { get; set; }
      
          [Field]
          public string InvoiceNumber { get; set; }
      }
      
  • Many to One
    • Many Foo objects can be related to a single Bar object.  This is common in a “look up” table.  For example if we have shipping addresses (Foo) and States (Bar)

      [Entity(KeyScheme.Identity)]
      public class ShipAddress
      {
          [Field(IsPrimaryKey = true)]
          public int AddressID { get; set; }
      
          [Field]
          public string Street { get; set; }
      
          [Reference(typeof(State), "StateID", ReferenceType = ReferenceType.ManyToOne)]
          public State State { get; set; }
      }
      
      [Entity(KeyScheme.Identity)]
      public class State
      {
          [Field(IsPrimaryKey = true)]
          public int StateID { get; set; }
      
          [Field]
          public string Name { get; set; }
      
          [Field]
          public string Abbreviation { get; set; }
      }
      
  • Many to Many
    • Many number of Foo objects can be related to any number of Bar objects. An example might be Books and Authors.  A Book (Foo) may have several Authors (Bar), likewise an Author may have written, or helped write, several Books.  In a typical RDBMS these require a third table. Note: ORM currently does not support this Relationship type.
  • One to One
    • For every one Foo, there is one Bar object.  From the ORM code perspective, this is the same as a One to Many so simply use a One to Many Reference definition Attribute.<!--EndFragment-->

Last edited Feb 18, 2013 at 7:25 PM by ctacke, version 9

Comments

davidecorazza Dec 13, 2016 at 8:43 AM 
Hello, I tried to create links between two tables with no success, even reading the codeplex articles and debugging the ORM code with visual studio.
I start with an empty database, and let the ORM create tables from the classes
I created two classes with a OneToMany relationship as shown above, then did this:

* CreateStore()
* AddType() // for all classes

The tables are created, the primitive types (e.g int, double, string) are created, but references are not; I expected something like an integer field that references the other table.
Did I miss something?