Thoughts from Dan Miser RSS 2.0
# Friday, September 23, 2005
As I mentioned in my previous post about LINQ and IDENTITY fields, I was getting an exception when trying to update back to the DB. I have since tracked this down to my guess in that post where it was due to the way that I constructed the customer object via foreach. Using that technique, it seems that it keeps a DataReader open longer than needed, which results in this error. I have a workaround, so I can at least share this sample to show you how related tables with IDENTITY fields work. If you want to see the exception, replace the line where I assign the cust variable with a "foreach (var cust in query)".

Things to note in this sample:

  • I am mixing and matching objects retrieved from the DB (cust) and locally created objects (o and od).
  • You can assign the Order object to the customer either by setting o.Customer to point to the customer, or using cust.Orders.Add(o). The same holds true for adding OrderDetail to Order.
  • IDENTITY fields are not initialized upon creation, but they are assigned the actual value that they got when getting inserted to the DB.
  • You can link tables together by either using the object references or the actual data field. For example, when building the OrderDetail object, I use od.ProductID to set the value that will get written to the DB. I could also have constructed/received a Product object from the DB and assigned od.Product to do the linking via object reference.
  • Linking between related tables is taken care of automatically, even when linked via IDENTITY fields


    class Program
    {
        static void Main(string[] args)
        {
            Northwind db = new Northwind(
                @"Data Source=(local);Initial Catalog=Northwind;Integrated Security=True");
            db.Log = Console.Out;

            var query = from c in db.Customers
                        where c.CustomerID == "ALFKI"
                        select c;

            Customer cust = query.ToArray()[0];
            Order o = new Order();
            o.Customer = cust;
            o.Freight = 23;
            Console.WriteLine("[PRE] Order.OrderID == " + o.OrderID);

            OrderDetail od = new OrderDetail();
            od.Order = o;
            od.Quantity = 1;
            od.ProductID = 1;
            Console.WriteLine("[PRE] OrderDetail.OrderID == " + od.OrderID);

            //o.OrderDetails.Add(od);
            //cust.Orders.Add(o);

            db.SubmitChanges();
            Console.WriteLine("[POST] Order.OrderID == " + o.OrderID);
            Console.WriteLine("[POST] OrderDetail.OrderID == " + od.OrderID);
        }
    }
Friday, September 23, 2005 7:54:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
LINQ
Tracked by:
http://9nc-information.info/44216008/company-registration-uk-company-formation-u... [Pingback]
http://9nb-information.info/31324833/index.html [Pingback]
http://9nn-information.info/15126973/index.html [Pingback]
http://9nx-information.info/23304532/jessica-mcclintock-wedding-dress-patterns.h... [Pingback]
http://9nd-information.info/24355648/index.html [Pingback]
http://9nd-information.info/73416292/index.html [Pingback]
http://9nc-information.info/62084231/official-tupelo-city-site.html [Pingback]
http://9nm-information.info/07142049/scotchcard-school-uniform-dress-shirts.html [Pingback]
http://9ne-information.info/62496619/index.html [Pingback]
http://9np-information.info/51124009/index.html [Pingback]
http://9nh-information.info/49071149/beaufort-house-for-sale.html [Pingback]
http://9ng-information.info/96408287/waterville-high-school-marching-song.html [Pingback]
http://9qe-information.info/07904560/raphsody.html [Pingback]
http://9ok-information.info/44407994/index.html [Pingback]
http://9qk-information.info/09770799/index.html [Pingback]
http://9qm-information.info/24399862/index.html [Pingback]
http://9oa-information.info/69556690/index.html [Pingback]
http://9oj-information.info/81805193/index.html [Pingback]
http://9on-information.info/05651356/index.html [Pingback]
http://9on-information.info/09602178/index.html [Pingback]
http://9oq-information.info/34183910/business-mix-tactics-and-strategies-in-bamk... [Pingback]
http://9rb-information.info/37639291/index.html [Pingback]
http://9se-information.info/14741047/index.html [Pingback]
http://9rf-information.info/17895022/index.html [Pingback]
http://9sq-information.info/29623648/resolve-update-asa.html [Pingback]
http://9re-information.info/96629691/index.html [Pingback]
http://9sp-information.info/66371250/index.html [Pingback]
http://9rl-information.info/57084620/index.html [Pingback]
http://9rh-information.info/88719911/index.html [Pingback]
http://9uafs-le-informazioni.info/51508306/organizzazione-matrimonio-fermo.html [Pingback]
http://9uaef-le-informazioni.info/20846523/index.html [Pingback]
http://9uaet-le-informazioni.info/94697309/index.html [Pingback]
http://9uafs-le-informazioni.info/66575460/lecce-roma.html [Pingback]
http://9uaej-le-informazioni.info/71341432/elicottero-volo-turistico.html [Pingback]
http://9uafl-le-informazioni.info/54949053/index.html [Pingback]
http://9uafm-le-informazioni.info/31308176/index.html [Pingback]
http://9uafq-le-informazioni.info/25488208/index.html [Pingback]
http://9uaep-le-informazioni.info/03260813/valle-varaita-piemonte-produttore-mob... [Pingback]
http://9uaha-le-informazioni.info/84370900/index.html [Pingback]
http://9uago-le-informazioni.info/97434843/brande-roderik.html [Pingback]
http://9uagr-le-informazioni.info/67899023/index.html [Pingback]
http://9uahm-le-informazioni.info/32895140/index.html [Pingback]
http://9uahd-le-informazioni.info/21547935/mana-moi-dix-mois.html [Pingback]
http://9uahr-le-informazioni.info/23382191/consulenza-energia-roma.html [Pingback]
http://9uags-le-informazioni.info/08378400/index.html [Pingback]
http://9uagj-le-informazioni.info/20715644/africa-unite-sito.html [Pingback]
http://9uaht-le-informazioni.info/00085356/situazione-contabile-al-bilancio.html [Pingback]
Navigation
Archive
<November 2014>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Dan Miser
Sign In
Statistics
Total Posts: 388
This Year: 0
This Month: 0
This Week: 0
Comments: 630
Themes
Pick a theme:
All Content © 2014, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)