Mapping Enumeration

Thread is closed for posting
5 posts, 0 answers
  1. Chris
    Chris avatar
    23 posts
    Member since:
    Feb 2009

    Posted 25 Nov 2009 Link to this post

    I seem to be having a problem with something that should be really simple, mapping an enumeration.  For some reason then value 0 is always stored in the db, no matter what value I assign to the entities field.

    Then enumeration is:
        Public Enum UomDistance As Integer
            Mm = 1
            Inch = 2
            Foot = 3
            Yard = 4
            Meter = 5
        End Enum

    In the Forward Mapping wizard both type and SQL Type are set to integer. In the app.config it is:
    <field name="_sizeOUM">
                  <extension key="db-column">
                    <extension key="db-sql-type" value="INT" />
                    <extension key="db-column-name" value="SizeUom" />
                  </extension>
    </field>
    I've search the docs and forums but don't see anything. Any thought about what might be causing this or how to dig deeper would be greatly appreciated.

    Thanks
    Chris



  2. IT-Als
    IT-Als avatar
    381 posts
    Member since:
    Sep 2008

    Posted 26 Nov 2009 Link to this post

    Hi Chris,

    This should work out-of-the-box. For instance I have (I know C#, but nevertheless):

        public enum InvoiceStateEnum
        {
            Created = 1,
            Printed = 2,
            Accepted = 3,
            Recorded = 4
        };

    and this enum is used directly as a field in my class like:

    public class SomePersistentClass
    {
       private InvoiceStateEnum;

       ...
       ...
       // Public properties goes here
    }

    In the forward mapping I do nothing else but giving the db column a meaningful name

    and it works like a charm.

    Regards

    Henrik

  3. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 26 Nov 2009 Link to this post

    Hello hgsdc,

    Each enumeration has an underlying type, which can be any integral type except char. By default this is int. In the provided example you use integer as underlying type, so the enumeration field is mapped to an integer column in the database.

    You could use the provided enumeration inside a class in the following way:
    Public Class EnumClassTest
        Public Sub New()
     
        End Sub
     
        Private id As Integer//pk autoinc
     
        Public Property Id() As Integer
            Get
                Return id
            End Get
            Set(ByVal value As Integer)
                id = value
            End Set
        End Property
     
        <Telerik.OpenAccess.DefaultFetchGroup(False)> Private name As String
     
        Public Property Name() As String
            Get
                Return name
            End Get
            Set(ByVal value As String)
                name = value
            End Set
        End Property
     
        Private distance As UomDistance
     
        Public Property Distance() As UomDistance
            Get
                Return distance
            End Get
            Set(ByVal value As UomDistance)
                distance = value
            End Set
        End Property
    End Class

    In order to insert new records in the database you could do the following:
    Dim obj As List(Of EnumClassTest) = New List(Of EnumClassTest)(New EnumClassTest() {New EnumClassTest() With {.Distance = UomDistance.Foot}, New EnumClassTest() With {.Distance = UomDistance.Meter}, New EnumClassTest() With { .Distance = UomDistance.Inch}, New EnumClassTest() With {.Distance = UomDistance.Yard}})
    scope.Transaction.Begin()
    scope.Add(obj)
    scope.Transaction.Commit()
    I hope the provided information will be helpful to you.

    All the best,
    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  4. IT-Als
    IT-Als avatar
    381 posts
    Member since:
    Sep 2008

    Posted 26 Nov 2009 Link to this post

    Hi Damyan,

    Yes, I know about the underlying type. I was giving the answer to Chris.

    Naturally, you have to give the property (enum) a value in your instance of the persistent class...and also within a transaction to store it.

    Regards

    Henrik
  5. Chris
    Chris avatar
    23 posts
    Member since:
    Feb 2009

    Posted 01 Dec 2009 Link to this post

    Thanks for the replies.

    I give everything a shot tomorrow.
Back to Top