I’m just getting started writing an app for WP (Mango), and am running into issues related to how to map a foreign key relationship for a lookup table using LINQ. The MSDN docs didnt seem to help (maybe i’m looking in the wrong place). Basically, I have 2 tables,
OrderType (this consists of lookup data referenced by Orders)
Orders (this will have a column called OrderTypeID pointing back to the above table)
basically a 1-to-many relationship.
Here’s how I’ve described in LINQ:
[Table(Name = "OrderTypes")]
public partial class OrderType
{
private Int16 _OrderTypeID;
private string _Name;
private string _ShortName;
[Column(Storage = "_OrderTypeID", DbType = "Int NOT NULL", IsPrimaryKey = true, CanBeNull=false)]
public Int16 Id {get; set;}
[Column(Storage = "_Name", DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
public string Name {get;set;}
[Column(Storage= "_ShortName", DbType= "NVarChar(50) NOT NULL", CanBeNull=false)]
public string ShortName {get;set;}
}
[Table(Name = "Orders")]
public partial class Order
{
private Int16 _OrderID;
private Int16 _OrderTypeID;
private string _Description;
[Column(Storage = "_OrderID", DbType = "Int NOT NULL", IsPrimaryKey = true)]
public Int16 Id {get;set;}
[Column(DbType = "Int", CanBeNull= true)]
public Int16 OrderTypeID { get; set; }
[Column(DbType = "NVarChar(1000) NULL", CanBeNull = true)]
public string Description { get; set; }
}
I confused re: how to use the EntityRef and/or EntitySet classes to describe this relationship in my code.
Thanks in advance.
EntitySetis on the “1” side, it contains the entities on the “many” side.EntityRefis on an entity of the “many” side, referring to the (parent) entity on the “1” side.In your case Order would have an
EntityRef<OrderType>. OrderType, in turn, could have anEntitySet<Order>, although I’d think the latter would not make much sense (semantically one would probably not say that an OrderType “has” orders, like an Order “has” order lines).In the Adventureworks database, when mapping a similar relationship, Product and ProductCategory, the Product class has a
ProductCategoryproperty: