Updating objects mapped at runtime

6 posts, 0 answers
  1. Ashley
    Ashley avatar
    30 posts
    Member since:
    Nov 2012

    Posted 22 Jul 2013 Link to this post

    Hi,

    Based on my discussion in this thread: http://www.telerik.com/community/forums/orm/development/create-fluent-mapping-at-runtime.aspx
    How would I go about updating an object in one of those tables?

    I have tried:
    var contextInstance = context.CreateInstance("FluentModel.testTable");
    context.Add(contextInstance);
    context.AttachCopy(contextInstance);
    contextInstance.SetFieldValue("id", 12);               
    contextInstance.SetFieldValue("testField", "test2");
    context.SaveChanges();

    But this crashes with the error:
    Insert of '1311643672-12' failed: Telerik.OpenAccess.RT.sql.SQLException: Violation of PRIMARY KEY constraint 'PK_testTable'. Cannot insert duplicate key in object 'dbo.testTable'. The duplicate key value is (12).

    I was hoping that AttachCopy would allow me to update. Please let me know how to achieve this.

    Regards,
    Ash
  2. Ashley
    Ashley avatar
    30 posts
    Member since:
    Nov 2012

    Posted 23 Jul 2013 Link to this post

    For that matter, Update, Delete and Read too. Currently, I only know how to add records.
  3. DevCraft banner
  4. Ashley
    Ashley avatar
    30 posts
    Member since:
    Nov 2012

    Posted 23 Jul 2013 Link to this post

    It also seems that compound keys are not supported for objects created this way, can you please confirm that? Also using a key generator with AutoInc doesn't seem to work either. Please provide me as much information as possible on how to correctly use this approach.
  5. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 23 Jul 2013 Link to this post

    Hello Ashley,

    If you want to edit an existing entity, you shouldn't create a new instance and add/attach it to the context. The AttachCopy() method is used when you want to attach objects that have been previously detached, or objects that have been retrieved outside the OpenAccessContext.

    If you want to Edit/Delete/Read an existing entity, you should retrieve it from the database using a Dynamic LINQ. When the object is retrieved you could get the field value with the FieldValue<T>() method and set a new value with the SetFieldValue<T>() method.

    There aren't any known issues when using AutoInc KeyGenerator with Artificial Types.
    For your convenience I have prepared a sample application which demonstrates How to: Read/Edit/Delete object of Artificial Type, also the Id column of the table is using Identity mechanism. Please find the sample attached.

    At the moment OpenAccess ORM doesn't support compound keys for Artificial Types. This feature request is in our backlog list and we will implement it for the next official release of OpenAccess - Q3 2013, expected in the end of October.

    I hope that helps.

    Regards,
    Boris Georgiev
    Telerik
    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
  6. Ashley
    Ashley avatar
    30 posts
    Member since:
    Nov 2012

    Posted 23 Jul 2013 Link to this post

    Hi Boris,

    Thanks for the feedback. Is there any way to change the name of the identity field from id?

    Regards,
    Ash
  7. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 24 Jul 2013 Link to this post

    Hello Ashley,

    At the moment when Artificial Type is declared, the identity field should be named "Id". This limitation will be removed when the support of compound keys for Artificial Types is introduced.

    The compound key support for Artificial Types and the removal of the restriction for Identity name will be available with the next Internal Build of OpenAccess ORM.

    I hope that helps.

    Regards,
    Boris Georgiev
    Telerik
    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
Back to Top
DevCraft banner