This is a migrated thread and some comments may be shown as answers.

LINQ to SQL to Open Access

3 Answers 76 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Gian Carlo Zamboni
Top achievements
Rank 1
Gian Carlo Zamboni asked on 30 Mar 2010, 07:07 PM
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.

3 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 31 Mar 2010, 08:15 AM
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
0
Serge
Telerik team
answered on 31 Mar 2010, 04:12 PM
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.
0
IT-Als
Top achievements
Rank 1
answered on 31 Mar 2010, 04:15 PM
Thanks Serge for pointing this out. I simply missed it. That's what happen when you write code in an forum text editor :-)

/Henrik
Tags
LINQ (LINQ specific questions)
Asked by
Gian Carlo Zamboni
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Serge
Telerik team
Share this question
or