This is a migrated thread and some comments may be shown as answers.
Mapping Enumeration
4 Answers 78 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris
Top achievements
Rank 1
Chris asked on 25 Nov 2009, 11:12 PM
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



4 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 26 Nov 2009, 08:12 AM
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

0
Damyan Bogoev
Telerik team
answered on 26 Nov 2009, 10:45 AM
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.
0
IT-Als
Top achievements
Rank 1
answered on 26 Nov 2009, 10:49 AM
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
0
Chris
Top achievements
Rank 1
answered on 01 Dec 2009, 06:11 AM
Thanks for the replies.

I give everything a shot tomorrow.
Tags
General Discussions
Asked by
Chris
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Chris
Top achievements
Rank 1
Share this question
or