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

Exception on Set/Get value of artificial fields with CLR type?

3 Answers 86 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alain
Top achievements
Rank 1
Alain asked on 22 Feb 2012, 10:51 AM
Hello all,

Open Access:2012.1.214
Visual Studio: 2010 SP1
Method: Fluent

We try to set/get the value of the artificial field "Age" of the class Person from the Open Access documentation:
Public Class Person
    Private _id As Integer
    Public Property Id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property
 
    Private _firstName As String
    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal value As String)
            _firstName = value
        End Set
    End Property
 
    Private _lastName As String
    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(ByVal value As String)
            _lastName = value
        End Set
    End Property
 
    Private _address As String
    Public Property Address() As String
        Get
            Return _address
        End Get
        Set(ByVal value As String)
            _address = value
        End Set
    End Property
End Class

For this, in the mapping of Person class, the artificial field "Age" is added:
Dim personConfiguraiton As New MappingConfiguration(Of Person)()
...
personConfiguraiton.HasArtificialPrimitiveProperty(Of Integer)("Age")

Now, we want to set a value for this field "Age" in an instance of the class "Person": We use the new extension SetFieldValue added in the Q12 of Open Access:
Dim nPerson as New Person
nPerson.SetFieldValue("Age", 12)

Open Access throws an exception:
PersistenceCapable instance is not managed by an IObjectScope.
Parameter name: persistentInstance

What is missing in our code to set/get correctly value of artificial fields with CLR types in code-only?

Note; same exception for FieldValue()

Thank you,
Alain

3 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 27 Feb 2012, 10:54 AM
Hi Alain,

 In order to use the new API you will need to first add your persistent object to your context. This API can only be used for objects that are being tracked by our context. You will need to do something similar to this:

context.Add(nPerson)

Greetings,
Petar
the Telerik team
Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
0
Alain
Top achievements
Rank 1
answered on 05 Mar 2012, 09:31 AM
Hi Petar,

Thank your for the information.

Two more questions regarding the set/get values of artificial fields:
  1. With your solution, if we add other objects to the Context, and we don't want anymore nPerson to be saved, must we use Context.Delete(nPerson) before Context.SaveChanges()?
  2. If we read an object of type Person like Dim nPerson as Person = Context.GetAll(Of Person).GetFirstOrDefault(...), and we try to access the artificial "Age" field with nPerson.FieldValue(Of Integer)("Age"), Open Access throws the same exception as initially explained. What we can do in this case to get the value of the artificial field of objects coming from Context?

 

Thank you.
Best regards,
Alain

0
PetarP
Telerik team
answered on 08 Mar 2012, 09:28 AM
Hi Alain,

 1. Yes this should be ok. Even if the object is not yet persisted you can remove it from the context as long as it is tracked by the same context to begin with.
2. I couldn't quite understand the problem here. Is the nPerson object coming from the database or is it not persisted yet?

Kind regards,
Petar
the Telerik team
Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
Tags
Getting Started
Asked by
Alain
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Alain
Top achievements
Rank 1
Share this question
or