0

Our goal

To be able to use a tool that generates automatically all the models from our DB (MySQL) with Null Safety enabled (Nullable Reference Types) and that lets us modify some properties' types and values without losing all the advantages from automatic generation. The DB fields cannot be changed.

Our current situation

We are using the ** Entity Framework scaffold command** to generate our DB Models.

We are using a custom class extending HbsCSharpEntityTypeGenerator from the Handlebars library to modify the properties' types (we are using this because we need to modify only properties from specific entities), and then using OnModelCreatingPartial(ModelBuilder modelBuilder) to convert the values when reading/saving to the DB, an example:

public partial class ContextX
    {
        partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<EntityX>()
                .Property(x => x.PropertyX)
                .HasConversion(
                    // In our model this property is an int? but the DB doesn't allow nulls, 
                    // in case of NULL we save 0 into the DB
                    x => x == null ? 0 : x,
                    // The DB field is int (using 0 as NULL), in case of 0 we set NULL in our model
                    x => x == 0 ? null : x);
        }
    }

The problem

As shown here, Entity Framework's property converter is not called when the types are for example int and int?, meaning that a ValueConverter<int, string> will be called every time we need to read/save to the DB, but a ValueConverter<int?, int> won't.

Our main reason behind using this approach was to use Nullable types and proper values in our models instead of wrong placeholders (empty strings, zeros, wrong default dates, etc), any solution matching the above requirements would be nice.

Thank you in advance.

Viswanatha Swamy
  • 699
  • 1
  • 10
  • 17
JSB
  • 95
  • 1
  • 8
  • But that means that you do have these "wrong placeholders" in the database. If they're harmful in C# then also in the database. Creating a data model in which columns are not nullable is not just a question of making them not nullable. It's crafting the model in a way that they can't even be null. Even then, you'll probably never get rid of nullable columns but then let them be null and don't introduce magic values. – Gert Arnold Aug 13 '21 at 07:19
  • @GertArnold we didn't create the DB tables, we are just consuming it, that DB is being used for many clients and can not be changed, we don't like the way its tables are created (not a single null in the DB, no FK etc), our only option here is to make our entities as good as we can with the tables already created so we can abstract ourselves from that hell in the bussiness logic (the only part that knows about that is the model creation part, which is way better than having to know that in every query we do...) – JSB Aug 13 '21 at 08:19
  • Yeah, I know the feeling... Still, with enough pressure everything becomes fluid, even databases set in stone. Either way, as long as EF doesn't solve this you have to resort to unappealing work-arounds like computed unmapped properties that do the conversion from the unconverted mapped property. – Gert Arnold Aug 13 '21 at 09:06

0 Answers0