Skip to content

MapInstantApis fails when key is not named id when using fluent interface [Bug] #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Wayne-Mather opened this issue Aug 20, 2022 · 5 comments

Comments

@Wayne-Mather
Copy link

Wayne-Mather commented Aug 20, 2022

When using the fluent interface and not marking your keys with the [Key] annotation and those keys are not named "id" then the _IdLookup dictionary will not be filled with the keys for the entity.

This is due to MapApiExtensions having id hard coded into it. I wonder if it would be easy to add a configuration option to define how the property is matched? For example {ClassName}Id or {ClassName}_Id

I did a quick proof of concept by adding the following code in line 26 of MapApiExtensions:

var theType = typeof(C);
var keyName = $"{theType.Name}Id";
var idProp = theType.GetProperty("id", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) ??
           theType.GetProperty(keyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) ??
	   theType.GetProperties().FirstOrDefault(p => p.CustomAttributes.Any(a => a.AttributeType == typeof(KeyAttribute)));

@csharpfritz
Copy link
Owner

csharpfritz commented Aug 20, 2022 via email

@Wayne-Mather
Copy link
Author

Hi,

Can you confirm you have an off by 1 error in your message? O => P 😁

@csharpfritz
Copy link
Owner

csharpfritz commented Aug 21, 2022 via email

@Wayne-Mather
Copy link
Author

I am doing something like the following:

app.MapInstantAPIs<MyContext>(config =>
{
	config.IncludeTable(db => db.Contacts, ApiMethodsToGenerate.All, "addressBook");
	config.PrimaryKeyMappingConvention(new List<string>() { "{ClassName}Id", "{ClassName}_Id", });
});

But it means changing Configuration from private to internal so that MapApiExtensions has access to it. Is this going to be a problem?

@Wayne-Mather
Copy link
Author

Pull request created

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants