diff --git a/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs b/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs index a1887ba235..7faa22bd55 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs @@ -11,7 +11,7 @@ public class AppDbContext : DbContext public DbSet TodoItemCollections { get; set; } public DbSet CamelCasedModels { get; set; } public DbSet
Articles { get; set; } - public DbSet Authors { get; set; } + public DbSet AuthorDifferentDbContextName { get; set; } public DbSet NonJsonApiResources { get; set; } public DbSet Users { get; set; } public DbSet PersonRoles { get; set; } diff --git a/src/Examples/NoEntityFrameworkExample/.gitignore b/src/Examples/NoEntityFrameworkExample/.gitignore index 0ca27f04e1..700191e656 100644 --- a/src/Examples/NoEntityFrameworkExample/.gitignore +++ b/src/Examples/NoEntityFrameworkExample/.gitignore @@ -22,6 +22,8 @@ bld/ [Bb]in/ [Oo]bj/ +Properties/launchSettings.json + # Visual Studio 2015 cache/options directory .vs/ # Uncomment if you have tasks that create the project's static files in wwwroot diff --git a/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json b/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json deleted file mode 100644 index 1dff6cfe69..0000000000 --- a/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:57181/", - "sslPort": 0 - } - }, - "profiles": { - "NoEntityFrameworkExample": { - "commandName": "Project", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "http://localhost:5000/" - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} \ No newline at end of file diff --git a/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs b/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs index 405fe64936..e025d4f7de 100644 --- a/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs @@ -218,7 +218,7 @@ private string GetResourceNameFromDbSetProperty(PropertyInfo property, Type reso // fallback to the established convention using the DbSet Property.Name // e.g DbSet FooBars { get; set; } => "foo-bars" - return _resourceNameFormatter.ApplyCasingConvention(property.Name); + return _resourceNameFormatter.FormatResourceName(resourceType); } private (bool isJsonApiResource, Type idType) GetIdType(Type resourceType) diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs index 1df5593fb0..6b6bb526a3 100644 --- a/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs @@ -240,7 +240,7 @@ public async Task Can_Create_Many_To_Many() var tag = _tagFaker.Generate(); var author = new Author(); context.Tags.Add(tag); - context.Authors.Add(author); + context.AuthorDifferentDbContextName.Add(author); await context.SaveChangesAsync(); var article = _articleFaker.Generate(); diff --git a/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs b/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs index d23b1f4d9d..20fa848b35 100644 --- a/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs +++ b/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs @@ -59,6 +59,27 @@ public void AddJsonApiInternals_Adds_All_Required_Services() Assert.NotNull(provider.GetService(typeof(RepositoryRelationshipUpdateHelper))); } + [Fact] + public void RegisterResource_DeviatingDbContextPropertyName_RegistersCorrectly() + { + // Arrange + var services = new ServiceCollection(); + + services.AddDbContext(options => options.UseInMemoryDatabase("UnitTestDb"), ServiceLifetime.Transient); + services.AddJsonApi(); + + // Act + // this is required because the DbContextResolver requires access to the current HttpContext + // to get the request scoped DbContext instance + services.AddScoped(); + var provider = services.BuildServiceProvider(); + var graph = provider.GetService(); + var resourceContext = graph.GetResourceContext(); + + // Assert + Assert.Equal("authors", resourceContext.ResourceName); + } + [Fact] public void AddResourceService_Registers_All_Shorthand_Service_Interfaces() { @@ -116,7 +137,7 @@ public void AddResourceService_Throws_If_Type_Does_Not_Implement_Any_Interfaces( } [Fact] - public void AddJsonApi_With_Context_Uses_DbSet_PropertyName_If_NoOtherSpecified() + public void AddJsonApi_With_Context_Uses_Resource_Type_Name_If_NoOtherSpecified() { // Arrange var services = new ServiceCollection(); @@ -130,7 +151,7 @@ public void AddJsonApi_With_Context_Uses_DbSet_PropertyName_If_NoOtherSpecified( var provider = services.BuildServiceProvider(); var resourceGraph = provider.GetService(); var resource = resourceGraph.GetResourceContext(typeof(IntResource)); - Assert.Equal("resource", resource.ResourceName); + Assert.Equal("int-resources", resource.ResourceName); } public class IntResource : Identifiable { }