You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you are using Entity Framework Core as your ORM, you can add an entire `DbContext` with one line.
14
41
@@ -20,9 +47,9 @@ public void ConfigureServices(IServiceCollection services)
20
47
}
21
48
```
22
49
23
-
### Defining Non-EF Resources
50
+
### Manual Specification
24
51
25
-
If you have resources that are not members of a DbContext, you can manually add them to the graph.
52
+
You can also manually construct the graph.
26
53
27
54
```c#
28
55
// Startup.cs
@@ -32,36 +59,30 @@ public void ConfigureServices(IServiceCollection services)
32
59
33
60
services.AddJsonApi(options=> {
34
61
options.BuildResourceGraph((builder) => {
35
-
// MyModel is the internal type
36
-
// "my-models" is the type alias exposed through the API
37
-
builder.AddResource<MyModel>("my-models");
62
+
builder.AddResource<MyModel>();
38
63
});
39
64
}, mvcBuilder);
40
65
}
41
66
```
42
67
43
-
### Resource Names
68
+
### Public Resource Type Name
44
69
45
-
If a `DbContext` is specified when adding the services, the context will be used to define the resources and their names. By default, these names will be hyphenated.
70
+
The public resource type name for is determined by the following criteria (in order of priority):
46
71
72
+
1. The model is decorated with a `ResourceAttribute`
47
73
```c#
48
-
publicclassAppDbContext : DbContext {
49
-
// this will be translated into "my-models"
50
-
publicDbSet<MyModel> MyModels { get; set; }
51
-
}
74
+
[Resource("my-models")]
75
+
publicclassMyModel : Identifiable { /* ... */ }
52
76
```
53
77
54
-
You can also specify your own resource name.
55
-
78
+
2. The `DbSet` is decorated with a `ResourceAttribute`
56
79
```c#
57
-
publicclassAppDbContext : DbContext {
58
-
// this will be translated into "someModels"
59
-
[Resource("someModels")]
60
-
publicDbSet<MyModel> MyModels { get; set; }
61
-
}
80
+
[Resource("my-models")]
81
+
publicDbSet<MyModel>MyModel { get; set; }
62
82
```
63
83
64
-
65
-
66
-
67
-
84
+
3. The configured naming convention (by default this is kebab-case).
At a minimum, resources must implement `IIdentifiable<TId>` where `TId` is the type of the primary key. The easiest way to do this is to inherit `Identifiable<TId>`.
4
-
5
-
```c#
6
-
publicclassPerson : Identifiable<Guid>
7
-
{ }
8
-
```
9
-
10
-
You can use the non-generic `Identifiable` if your primary key is an integer.
11
-
12
-
```c#
13
-
publicclassPerson : Identifiable
14
-
{ }
15
-
16
-
// is the same as
17
-
18
-
publicclassPerson : Identifiable<int>
19
-
{ }
20
-
```
21
-
22
-
If you need to hang annotations or attributes on the `Id` property,
23
-
you can override the virtual property.
3
+
If you want an attribute on your model to be publicly available, add the `AttrAttribute`.
24
4
25
5
```c#
26
6
publicclassPerson : Identifiable
27
-
{
28
-
[Key]
29
-
[Column("person_id")]
30
-
publicoverrideintId { get; set; }
7
+
{
8
+
[Attr]
9
+
publicstringFirstName { get; set; }
31
10
}
32
11
```
33
12
34
-
If your resource must inherit from another class,
35
-
you can always implement the interface yourself.
36
-
In this example, `ApplicationUser` inherits `IdentityUser`
37
-
which already contains an Id property of type string.
13
+
## Public name
38
14
15
+
There are two ways the public attribute name is determined:
16
+
1. By convention, specified by @JsonApiDotNetCore.Configuration.JsonApiOptions#JsonApiDotNetCore_Configuration_JsonApiOptions_ResourceNameFormatter
Dependent relationships should contain a property in the form `{RelationshipName}Id`. For example, a TodoItem may have an Owner and so the Id attribute should be OwnerId.
At a minimum, resources must implement `IIdentifiable<TId>` where `TId` is the type of the primary key. The easiest way to do this is to inherit `Identifiable<TId>`.
4
+
5
+
```c#
6
+
publicclassPerson : Identifiable<Guid>
7
+
{ }
8
+
```
9
+
10
+
You can use the non-generic `Identifiable` if your primary key is an integer.
11
+
12
+
```c#
13
+
publicclassPerson : Identifiable
14
+
{ }
15
+
16
+
// is the same as
17
+
18
+
publicclassPerson : Identifiable<int>
19
+
{ }
20
+
```
21
+
22
+
If you need to hang annotations or attributes on the `Id` property,
23
+
you can override the virtual property.
24
+
25
+
```c#
26
+
publicclassPerson : Identifiable
27
+
{
28
+
[Key]
29
+
[Column("person_id")]
30
+
publicoverrideintId { get; set; }
31
+
}
32
+
```
33
+
34
+
If your resource must inherit from another class,
35
+
you can always implement the interface yourself.
36
+
In this example, `ApplicationUser` inherits `IdentityUser`
37
+
which already contains an Id property of type string.
Currently EntityFrameworkCore [does not support](https://github.com/aspnet/EntityFrameworkCore/issues/1368) Many-to-Many relationships without a join entity.
42
+
For this reason, we have decided to fill this gap by allowing applications to declare a relationships as `HasManyThrough`.
43
+
JsonApiDotNetCore will expose this attribute to the client the same way as any other `HasMany` attribute.
44
+
However, under the covers it will use the join type and EntityFramework's APIs to get and set the relationship.
45
+
46
+
```c#
47
+
publicclassArticle : Identifiable
48
+
{
49
+
[NotMapped] // ← tells EF to ignore this property
50
+
[HasManyThrough(nameof(ArticleTags))] // ← tells JADNC to use this as an alias to ArticleTags.Tags
0 commit comments