MediatR.CommandQuery icon indicating copy to clipboard operation
MediatR.CommandQuery copied to clipboard

Supporting Key names other than "Id"

Open zachrybaker opened this issue 3 years ago • 2 comments

First off, this is a very convenient library, especially combined with the EFC Generator, especially when your database design is "ideal."

@pwelter34 I'm curious if you have considered making it work for tables whose PK is not named "Id." Let's ignore compound keys for a moment. While Id is the EF convention, it is not an EF requirement. Wouldn't it be nice if it wasn't a requirement here?

From what I can tell, the only place that we run into trouble is in the EntityDataContextHandlerBase's Read method. The Where clause has a linq expression which is server-evaluated looking for an "Id" field. While I could add a view or a computed column to get around this hard-coded property name, folks probably prefer to keep their db as pure as possible.

One thought is that if all these EFC-facing handlers and even EntityDataContextHandlerBase were themselves implementations of generic interfaces, perhaps we could substitute other implementations to get us the desired key name in Read implentation. Just a thought.

Otherwise, the "Id" name is not an issue without code workarounds. The create/delete/update handlers don't suffer this above issue on the EF side. On the model-facing side, this is a viable workaround at the entity level to avoid issues:

    [NotMapped]
    public string Id { get => MyActualKey; set => MyActualKey= value; }

zachrybaker avatar Jan 06 '22 23:01 zachrybaker