You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When mapping enums to enum types in Postgres as described here:
The ToString on a mapped enum in query select is translated, and we get the Postgres enum type text.
I expected this configuration to be transparent to the application and just a storage concern, but it affects the application code due to the changed behavior.
Should the documentation on the enum configuration mention this?
I am not stating that this is wrong behavior, just that it could easily be overlooked when starting to use enum types.
I'm not sure I understand - you're proposing to document that when ToString() is called on an enum in a LINQ query, that's translated to a cast to PG text? What other behavior would you expect here, and how is this different from applying ToString() on any other type (e.g. an int)?
When we use MapEnum the behavior of the ToString() method in a LINQ query changes.
public enum Color { Red = 1, Green, Blue , ..}
public class Entity {
public Color Color { get; set; }
}
Without MapEnum:
Calling ToString() on the C# enum within a LINQ query translates to the enum's string representation as defined in the C# code. For example, assume entity has color red, then entitity.Color.ToString() would return "Red" not the integer value stored in database.
With MapEnum:
Calling ToString() on the mapped enum within a LINQ query translates to the enum's text value in the database. For example, if Colors is mapped to a Postgres enum and entity.Color.ToString() is called in a LINQ query, it will return the text "red" (entity color is red as above example).
This change in behavior can be unexpected for developers who assume that MapEnum is purely for storage purposes.
var data = db.Entities.Select(t => t.Color.ToString()).ToList();
// without MapEnum
// data = [ "Red", "Green", "Blue" ,..]
// with MapEnum
// data = [ "red", "green", "blue" ,..]
When mapping enums to enum types in Postgres as described here:
The
ToString
on a mapped enum in query select is translated, and we get the Postgres enum type text.I expected this configuration to be transparent to the application and just a storage concern, but it affects the application code due to the changed behavior.
Should the documentation on the enum configuration mention this?
I am not stating that this is wrong behavior, just that it could easily be overlooked when starting to use enum types.
The translation was probably introduced here.
The text was updated successfully, but these errors were encountered: