LINQ to SQL to Open Access

4 posts, 0 answers
  1. Gian Carlo Zamboni
    Gian Carlo Zamboni avatar
    29 posts
    Member since:
    Mar 2007

    Posted 30 Mar 2010 Link to this post

    Hi,

    I want to know how to translate the following linq code to telerik openaccess code with scope:

    LINQ TO SQL
    using (ModelDataContext db = new ModelDataContext()) 
     
        db.Transaction = db.Connection.BeginTransaction(); 
         
        Invoice inv = new Invoice() 
        { 
            TotalAmount = 1000 
            Type = "A" 
        }; 
        db.Invoices.InsertOnSubmit(inv); 
         
        db.SubmitChanges(); 
     
        InvoiceDetail det = new InvoiceDetail() 
        { 
            IdInvoice = inv.Id, 
            Product = "Product A"
            Quantity = 1 
            Price =  1000 
        }; 
        db.InvoiceDetails.InsertOnSubmit(det); 
        db.SubmitChanges(); 
     
        db.Transaction.Commit(); 
         

    OpenAccess

    using(IObjectScope scope = ObjectsLibrary.GetNewObjectScope()) 
         ?????? 

    Thannks.

  2. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 31 Mar 2010 Link to this post

    Hi Gian,

    It really depends on how your model of Invoice and InvoiceDetail looks like. In the following I assume that there's a one (the invoice) to many (list of invoice details) relationship between the two. Thus there's also a list of InvoiceDetail instances on the Invoice class, say a property Invoice.Details of the type IList<InvoiceDetail> on the Invoice class

    You can do it mostly the same way as you already do... here are the changes:

    scope.Transaction.Begin()

    Invoice inv = new Invoice();
    // Don't now if you can do inline initialization of properties here...try it...otherwise use
    inv.TotalAmount = 1000;
    int.Type = "A";

    InvoiceDetail det = new InvoiceDetail()
    // Don't now if you can do inline initialization of properties here...try it...otherwise use
    det.Product = "Product A";
    det.Quantity = 1;
    det.Price = 1000;

    // Connect invoice and invoice detail
    inv.Details.Add(det);

    scope.Transaction.Commit();

    This all assumes this:
    That at the table level you will have a foreign key in the InvoiceDetail table that references the Invoice table. OA will use this information to generate the Invoice.Details property during reverse mapping.

    Regards

    Henrik
  3. DevCraft banner
  4. Serge
    Admin
    Serge avatar
    375 posts

    Posted 31 Mar 2010 Link to this post

    Hello Henrik and Hello Gian,

     I will only add that you can in fact do an inline initialization as it is a C# feature. Also there is a small glitch in the Henrik's code - it does not add  the new invoice object to the scope. Just before committing you have to add this line :

    scope.Add(inv);

    Hope this helps.

    Regards,
    Serge
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 31 Mar 2010 Link to this post

    Thanks Serge for pointing this out. I simply missed it. That's what happen when you write code in an forum text editor :-)

    /Henrik
Back to Top