Question about LINQ 1-n

5 posts, 0 answers
  1. Daniel Plomp
    Daniel Plomp avatar
    130 posts
    Member since:
    Feb 2004

    Posted 12 May 2009 Link to this post

    Hi all,

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

    1 // Create a new instance of an offer and fill the properties  
    2             Offer offer = new Offer();  
    3             offer.Username = o.Username;  
    4             offer.Material.MaterialId = o.Material.MaterialId;  
    5             offer.MaterialCompletion.MaterialCompletionId = o.MaterialCompletion.MaterialCompletionId;  
    6             offer.Washup.WashupId = o.Washup.WashupId;  
    7             offer.WashupCraneHoles = o.WashupCraneHoles;  
    8             offer.HotPlateSaving.HotPlateSavingId = o.HotPlateSaving.HotPlateSavingId;  
    9             offer.KitchenSetup.KitchenSetupId = o.KitchenSetup.KitchenSetupId;  
    10               
    11             // Add the new instance to the database  
    12             Scope.Transaction.Begin();  
    13             Scope.Add(offer);  
    14             Scope.Transaction.Commit();  
    15  
    16             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
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 13 May 2009 Link to this post

    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.
  3. DevCraft banner
  4. Daniel Plomp
    Daniel Plomp avatar
    130 posts
    Member since:
    Feb 2004

    Posted 13 May 2009 Link to this post

    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
  5. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 14 May 2009 Link to this post

    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.
  6. Daniel Plomp
    Daniel Plomp avatar
    130 posts
    Member since:
    Feb 2004

    Posted 14 May 2009 Link to this post

    Hi Alexander,

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

    Greetings -Daniel-
Back to Top
DevCraft banner