AdventureWorks mapping: Entity references

by Alexey Shirshov April 13, 2009 09:48

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

Add comment


(Will show your Gravatar icon)  

Enter the word
captcha word
(hear it spoken)


  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen | Modified by Mooglegiant

The Author

My name is Alexey Shirshov. I'm a professional developer with wide specialization. I prefer VB.NET to C#, I hate ASP.NET but there is no better than it. You can contact me by this page.