In the previous example, we create Store entity which has SalesPersonID property type of int. This is a database reference to SalesPerson object. Let's implement this reference in our model.
Obviously, we have to create SalesPerson entity.
[Entity("Sales", "SalesPerson", "1")]
public class SalesPerson
{
[EntityProperty("SalesPersonID", Field2DbRelations.PK)]
public int ID { get; set; }
public decimal SalesQuota { get; set; }
}
The next and the last thing we have to do - change type and name of the corresponding property in Store class.
[EntityProperty("SalesPersonID")]
public SalesPerson SalesPerson { get; set; }
That is all. Here is the code of all classes.
[Entity("Sales", "Store", "1")]
public class Store4
{
[EntityProperty("CustomerID", Field2DbRelations.PK)]
public int ID { get; set; }
public string Name { get; set; }
public DateTime ModifiedDate { get; set; }
public XmlDocument Demographics { get; set; }
[EntityProperty("rowguid", Field2DbRelations.RowVersion)]
public Guid Timestamp { get; protected set; }
[EntityProperty("SalesPersonID")]
public SalesPerson SalesPerson { get; set; }
public static QueryCmd Query
{
get
{
return new QueryCmd(exam1sharp.Properties.Settings.Default.connString)
.From(typeof(Store4))
.Select(typeof(Store4));
}
}
}
[Entity("Sales", "SalesPerson", "1")]
public class SalesPerson
{
[EntityProperty("SalesPersonID", Field2DbRelations.PK)]
public int ID { get; set; }
public decimal SalesQuota { get; set; }
}
And the program prints sales person quota information.
static void Main(string[] args)
{
foreach (Store4 s in Store4.Query
.Where(Ctor.prop(typeof(Store4), "Name").like("A%"))
.ToList())
{
Console.WriteLine("Store id: {0}, name: {1}, sales person quota: {2}", s.ID, s.Name, s.SalesPerson.SalesQuota);
}
}