To store enums as strings in the DB, I am using:
builder.Entity("Foo")
.HasProperty("Bar")
.HasConversion<string>()
However, if there is an entry in the DB that is not convertible, then an exception is thrown when fetching the record(s).
I want to just ignore those record(s) instead.
The only convention I could come up with is ensuring that all enums have a default value that is otherwise unused and then set up a query filter to exclude default. However, this would require a fairly extensive refactor of the codebase that I’d like to avoid.
Is there an alternative, such as custom converter that would cause EF to skip the record if it is otherwise unconvertable? To clarify, not skip the field (i.e. default or null), but skip the entire record.
Examples:
public class Foo
{
public int Id {get; set;}
public string Name {get; set;}
public Bar Bar {get; set;}
}
public enum Bar
{
A,
B
}
Id | Name | Bar |
---|---|---|
1 | Name1 | A |
2 | Name2 | B |
3 | Name3 | C |
var result = _dbContext.Foo.ToList();
Desired outcome is that result
would only have 2 entries (records 1 & 2) not all 3 since C
is not defined in Bar
.