none
EF 5 RC enum database column type RRS feed

  • Question

  • It appears that the database column type needs to be an int for enum's to work correctly.  I get a casting error when the type is a smallint.  Is there a way to get this to work without converting the database table.

    Thanks

    Tuesday, July 24, 2012 9:00 PM

Answers

  • Hi rlahnum,

    If you are using code-first, please let your enum type derive "short". Below is the code, please refer to it.

    class Program
        {
            static void Main(string[] args)
            {
                using (Context context = new Context())
                {
                    context.Database.Create();
                }
            }
        }
    
        public enum TestType:short
        { 
            testOne,
            testTwo,
            testThree
        }
    
        class Test
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public TestType type { get; set; }
        }
    
        class Context : DbContext
        {
            public DbSet<Test> testSet { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Test>().Property(x => x.type).HasColumnType("smallint");
            }
        }

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, July 26, 2012 11:58 AM
    Moderator

All replies

  • Hi rlahnum,

    Welcome to MSDN Forum.

    I have tested the smallint type in the database, it works well. Below is my steps, please check if there're any difference between ours.

    1. Create test table in the database

    create table EnumTest
    (
      id int primary key identity,
      name nvarchar(50) not null,
      test smallint not null
    )

    2. Create a ConsoleApplication which target to .NET4.5.

    3. Execute command 'install-package entityframework -pre' in the 'Package Manager Console' to install EF5.0.

    4. Add a EDMX file which contains the test table into the project.

    5. Right click on the 'test' property and select 'Convert to Enum'.

    6. Specify a name for the Enum type, the 'Underlying Type' is 'Int16', add the members of the enum type, finally, click OK. Until this step, we can see the enum type which we have added under the Model.Designer.cs file.

    7. Write the code in the Main method to insert a record.

    using(TestEntities context = new TestEntitites())
    {
      EnumTest et = new EnumTest();
      et.name = "test";
      et.test = testEnum.test1;
      context.AddToEnumTests(et);
      context.SaveChanges();
    }

    After complete the steps above, the record appears in the database, no exception is thrown.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, July 25, 2012 6:21 AM
    Moderator
  • I am using code first approach with the fluent api so I am not sure how to accomplish your step 5.
    Wednesday, July 25, 2012 12:07 PM
  • Hi rlahnum,

    If you are using code-first, please let your enum type derive "short". Below is the code, please refer to it.

    class Program
        {
            static void Main(string[] args)
            {
                using (Context context = new Context())
                {
                    context.Database.Create();
                }
            }
        }
    
        public enum TestType:short
        { 
            testOne,
            testTwo,
            testThree
        }
    
        class Test
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public TestType type { get; set; }
        }
    
        class Context : DbContext
        {
            public DbSet<Test> testSet { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Test>().Property(x => x.type).HasColumnType("smallint");
            }
        }

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, July 26, 2012 11:58 AM
    Moderator
  • That worked great.

    Thanks.

    Thursday, July 26, 2012 12:29 PM