Not mapping an EF Core DB-First enum property
I have scaffolded my Db Context. My entity has a
I am making a partial class which is:
Yet, when I do
_context.Hosts.ToList()
, it does attempt to select a column named "Status". How do I prevent this? The NotMapped
attribute does not seem to be working.11 Replies
[JsonIgnore]
also doesn't work
I tried something like
But this gives me errors that the column status is referenced twice
for the time being I'm making a script that edits the generated files + adds conversionto me is unclear what behavior you would like from
_context.Hosts.ToList()
to create a list of Hosts, including all properties in the entity. Currently the generated query also includes
[Status]
which does not exist as a column. I want EF to not include this fake column in the query generationyou can also use simply
.HasConversion<string>());
i believe
because enum is one of the basic supported types
even if there is a EnumToStringConverter<>
you don't need it
(ok the name is "pre-defined conversions")Typically the approach I use for this is having a seperation of my database models versus my "view" models, and perform a .Select to translate between the two.
This translation logic I would put on the view model instead with its Read/Write methods
I would have the following on
HostModel
and
And then you can do stuff like
or
yeah but enums are supported by ef
That could work, though modifying entities isn't that easy then anymore
its considered a pretty common practice, since you typically dont want to expose your entire entity to the front end. Its typically better to have an intermediary "view model" or "DTO" that acts as the translator between FE <-> BE.
This serves the role of explicitly stating what columns you select and whatnot, and tightens up the scope of what specifically is exposed to the FE and what the FE can modify, and also allows you to use values that might not exist on the db at all.
For example you may have
FirstName
and LastName
as seperate columns on the BE, but you expose just a simple readonly FullName
on your DTO to the front end, etc etcI do have separate DTOs for the output of my endpoints, but that's done on the final line of my service methods. I don't think it's valuable to convert entities into models and then models into entities again. This service is pretty small
I settled on adding this to my powershell script that re-creates the db scaffold:
Pretty nifty
replaces
with
with the name of the filename substituted to [X]
and then in the
OnModelCreatingPartial
,
I add
might eventually make it reflection based, to look for properties named Status of type enum, and add this automatically, but for now I've hardcoded it for two entitiesjust for the record, you do know that entity framework just straight up supports enums as a property out of the box right?
that's what my replace script does
it changes the generated string into an enum
my entities are generated DB first
by
dotnet ef dbcontext scaffold