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

4 posts, 0 answers
  1. Alain
    Alain avatar
    33 posts
    Member since:
    Feb 2009

    Posted 22 Feb 2012 Link to this post

    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
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 27 Feb 2012 Link to this post

    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 >>
  3. DevCraft banner
  4. Alain
    Alain avatar
    33 posts
    Member since:
    Feb 2009

    Posted 05 Mar 2012 Link to this post

    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

  5. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 08 Mar 2012 Link to this post

    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 >>
Back to Top