CreateDetachedCopy Stream

6 posts, 2 answers
  1. Alexander
    Alexander avatar
    5 posts
    Member since:
    Jan 2014

    Posted 03 Jan 2015 Link to this post

    I have the following code snippet which creates a detached copy of a contact...

    EditableItem = Context.CreateDetachedCopy(SelectedItem, con => con.Picture,
                                                                        con => con.MainAddress,     
                                                                        con => con.BillingAddress,
                                                                        con => con.BankAccount,
                                                                        con => con.Organization,
                                                                        con => con.Discount);

    Now if I look at my copy all data is there except the picture of the contact which is
    a binary. I read the documentation about "Working with Streams" and "Attaching and Detaching"
    but I wasn't able to get this up an running. So it would be great if you could help me
    or provide me a sample where a binary stream is detached and then attached to the context.

    Regards Alex
  2. Answer
    Boyan
    Admin
    Boyan avatar
    100 posts

    Posted 07 Jan 2015 Link to this post

    Hi Alexander,

    As shown in the Working with Streams documentation article, a stream is read at multiple passes retrieving the data at chunks. Reading this stream is done manually and it requires calls to the database meaning that reading it though a detached object would result in InvalidOperationException with the following explanation: Instance is detached, no database operation can be made.

    This means that if you would like to detach an object along with its BinaryStream property, the first think you have to do is to read and manually store the binary data before the object is detached. Once you have stored the data you could then detach the object. Both how to read and then re-attach such an object is demonstrated in the Working with Streams documentation article (also linked above).

    Alternative approach could be to map this column to byte[] property instead of BinaryStream. This would allow you to include it in the properties to be detached (as you have shown that you do) and detach it. Please note that detaching binary column in such way could lead to performance degradation depending on the size of the retrieved data. Also taking this approach you will lose the ability to easily stream the data.

    I hope this is helpful. Do not hesitate to get back to us if you have any more questions or if you need any further assistance.

    Regards,
    Boyan
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Alexander
    Alexander avatar
    5 posts
    Member since:
    Jan 2014

    Posted 12 Jan 2015 in reply to Boyan Link to this post

    Hello Boyan,

    I changed the mapping of the column to byte[] but still it doesn't work. 
    So within the original object the binary property field is not null and the picture is shown  
    As soon as I create a detached copy of the object the binary data field is null. All other properties
    are loaded correct.

    Regards Alex
  5. Alexander
    Alexander avatar
    5 posts
    Member since:
    Jan 2014

    Posted 12 Jan 2015 in reply to Boyan Link to this post

    I changed the mapping of the column to byte[] but still it doesn't work.
    So within the original object the binary property field is not null and the picture is shown  
    As soon as I create a detached copy of the object the binary data field is null. All other properties
    are loaded correct.
  6. Alexander
    Alexander avatar
    5 posts
    Member since:
    Jan 2014

    Posted 12 Jan 2015 in reply to Boyan Link to this post

    Found the error. Thanks for your help!
  7. Answer
    Boyan
    Admin
    Boyan avatar
    100 posts

    Posted 13 Jan 2015 Link to this post

    Hello Alexander,

    We are glad to hear that you have resolved the issue on your side.

    Do not hesitate to get back to us if you need any further assistance.

    Regards,
    Boyan
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top
DevCraft banner