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

Question about LINQ 1-n

4 Answers 68 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.
Daniel Plomp
Top achievements
Rank 2
Daniel Plomp asked on 12 May 2009, 01:33 PM
Hi all,

I'm trying to create a record inside a database table, named 'Offer'.
I'm used to doing this as follows:

// Create a new instance of an offer and fill the properties  
            Offer offer = new Offer();  
            offer.Username = o.Username;  
            offer.Material.MaterialId = o.Material.MaterialId;  
            offer.MaterialCompletion.MaterialCompletionId = o.MaterialCompletion.MaterialCompletionId;  
            offer.Washup.WashupId = o.Washup.WashupId;  
            offer.WashupCraneHoles = o.WashupCraneHoles;  
            offer.HotPlateSaving.HotPlateSavingId = o.HotPlateSaving.HotPlateSavingId;  
            offer.KitchenSetup.KitchenSetupId = o.KitchenSetup.KitchenSetupId;  
              
            // Add the new instance to the database  
            Scope.Transaction.Begin();  
            Scope.Add(offer);  
            Scope.Transaction.Commit();  
 
            return offer.OfferId; 

For some reason I´m getting an exception when executing line 4.
I know this works with the entity framework, but will this work with ORM?

Anybody has an idea?

Daniel

4 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 13 May 2009, 06:36 AM
Hi Daniel,

You do not need to set manually the ids. Just assign the actual object to the referencing property:
offer.Material = o.Material; 
The ids will be managed automatically.

All the best,
Alexander
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Daniel Plomp
Top achievements
Rank 2
answered on 13 May 2009, 06:47 AM
Hi Alexander,

I understand this. Maybe my example wasn't clear enough. The problem is that I don't have, in my case, a material object. I only can pass ID's from the webpage. I am aware that passing an object would work well, but that isn't the case.

Daniel
0
Alexander
Telerik team
answered on 14 May 2009, 07:25 AM
Hi Daniel,

There are two possible approaches in OpenAccess for handling relations between objects. One of them is having a reference to the other object as in the example you posted. In this case when you have the ID of the referenced object, you can load the object by its id and assign it to the referencing object. This should work for you:
offer.Material = (Material) scope.GetObjectById(Database.OID.ParseObjectId(typeof(Material), o.Material.MaterialId.ToString()));  
I suggest you to use this approach.

The other way is not having a reference to the referenced object at all, but a field with its ID only. This can be achieved (if you are using reverse mapping) by removing the reference that is generated by default in the Reverse mapping wizard. If doing so, a field for the foreign ID will be created. In your case you will get a MaterialID property instead of the Material reference. Then you can set the ID directly to that property as you have used to do. But this complicates the work with the related object - you will have to load it explicitly every time when you want to use it. That is why I suggested you to use the first approach.

Greetings,
Alexander
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Daniel Plomp
Top achievements
Rank 2
answered on 14 May 2009, 07:55 AM
Hi Alexander,

Thanks for the explanation. It is clear for me know.

Greetings -Daniel-
Tags
LINQ (LINQ specific questions)
Asked by
Daniel Plomp
Top achievements
Rank 2
Answers by
Alexander
Telerik team
Daniel Plomp
Top achievements
Rank 2
Share this question
or