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

Updating objects mapped at runtime

5 Answers 66 Views
Development (API, general 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.
Ashley
Top achievements
Rank 1
Ashley asked on 22 Jul 2013, 02:30 PM
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

5 Answers, 1 is accepted

Sort by
0
Ashley
Top achievements
Rank 1
answered on 23 Jul 2013, 07:20 AM
For that matter, Update, Delete and Read too. Currently, I only know how to add records.
0
Ashley
Top achievements
Rank 1
answered on 23 Jul 2013, 09:10 AM
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.
0
Boris Georgiev
Telerik team
answered on 23 Jul 2013, 01:18 PM
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.
0
Ashley
Top achievements
Rank 1
answered on 23 Jul 2013, 01:21 PM
Hi Boris,

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

Regards,
Ash
0
Boris Georgiev
Telerik team
answered on 24 Jul 2013, 12:27 PM
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.
Tags
Development (API, general questions)
Asked by
Ashley
Top achievements
Rank 1
Answers by
Ashley
Top achievements
Rank 1
Boris Georgiev
Telerik team
Share this question
or