locked
Trim trailing spaces from retrieved strings in EF Core RRS feed

  • Question

  • User-1262787652 posted

    ASP.NET MVC Core 5 application uses Npgsql Entity Framework Core Data Provider to get data from Postgres database using Entity Framework Core. Columns in database are defined as CHAR(n) type, like

        create table prpalk (
        sfirmanimi char(100);
        )
    

    Column types cannot changed to varchar.

    Using EF commands like string nimi = ctx.Prpalks.Single().Sfirmanimi; to get data, strings in application contain also trailing spaces. How to remove trailing spaces automatically ?

    Is there some event in EF Core which can used to trim all string columns when returned to application ?

    I havent found such setting in EF data provider, Npgsql or Postgres database.

    Column types cannot changed to varchar type due to compatibility with existing legacy application.

    Thursday, December 31, 2020 7:03 PM

Answers

  • User1120430333 posted

    There is nothing I know about from a ORM, EF, standpoint that can do what you are seeking. The key would be to address whatever code that is persisting the data and trim it before it is saved.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 3, 2021 6:32 PM

All replies

  • User1120430333 posted

    ctx.Prpalks.Single().Sfirmanimi.Trim();

    You can't do the above?

    Friday, January 1, 2021 6:02 PM
  • User-1262787652 posted

    Hi!

    Properties are used hundreds of places in code. Some methods returns list of objects. Sometimes property names are not hard-coded. 

    Object can contain references to related objects which can also contain triling spaces.

    Objects are also passed to javascript and xml serialisezers. They also serialize trailing spaces. 

    Trimming trailing spaces in all those cases this requires creating routine which walks over object trees and trims spaces from string properties. This is lot of work.

    Friday, January 1, 2021 8:46 PM
  • User1120430333 posted

    There is nothing I know about from a ORM, EF, standpoint that can do what you are seeking. The key would be to address whatever code that is persisting the data and trim it before it is saved.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 3, 2021 6:32 PM