0

I have configured various entities in my DbContext class to convert the enum value to a string when saving to the database eg.

modelBuilder.Entity<TestObject>(builder =>
{
   builder.ToTable("TestObject", "test");
   builder.Property(x => x.MyEnum).HasConversion<string>();
}

However all of these enum values are being saved as the numeric value behind the enum.

I've tried checking the strings aren't being truncated by the max lengths on the columns in the database but the enum string values are definitely all valid lengths.

At this point I'm not sure where else to check, any help is appreciated!

3
  • What type is the column in database? Commented Aug 3, 2022 at 13:02
  • Did you check this solution: stackoverflow.com/questions/32542356/… Commented Aug 3, 2022 at 13:12
  • @Rafalon it's varchar(20) and the longest string value in the enum is 8 characters long Commented Aug 3, 2022 at 13:48

1 Answer 1

0

The issue was I was using a stored procedure for the insert and didn't realise I was passing the enum value to the SQL parameter in as MyTestEnum.TestValue.

Passing in MyTestEnum.TestValue.ToString() as the SQL parameter fixed the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.