2

Let's say we have first-names stored in all lower-case in a column in the Person table in the database, like: "john", "jane", "mary", etc.

When I load the first names into the Person entity (for example, to fulfill a query), I want to transform the first names so that the first letter is capitalized. So "John", "Jane" etc.

How can I do that using C#/Entity Framework. We also use Auto-Mapper.

I guess in general, how would I transform data from a table's record before loading the record into an entity?

James Burani
  • 149
  • 1
  • 8
  • Does [this](https://stackoverflow.com/a/19377226/9338546) answer the data transformation part of your question? – Collen Oct 09 '20 at 20:46
  • So what have you tried so far? Please, show us your code and explain where did you get stuck? – Selim Yildiz Oct 09 '20 at 21:26
  • You could create a `[NotMapped]` property that capitalizes the first char then pass that value around when needing to display results. This way there is no chance the data in the db itself will be effected. – jaabh Oct 09 '20 at 22:06
  • string result = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(textToChange); – henoc salinas Oct 10 '20 at 18:45

1 Answers1

1

you can add a notMapped property like:

        [NotMapped]
        public string NameTitleCase
        {
            get
            {
                return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(this.Name.ToLower());
            }
        }

remember add

using System.Globalization;
henoc salinas
  • 1,044
  • 1
  • 9
  • 20