Skip to content

Commit 7c104c2

Browse files
committed
add coursestudent resource corresponding to the coursestudent many-to-many entity required by ef core
1 parent 664d35e commit 7c104c2

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/Examples/JsonApiDotNetCoreExample/Migrations/20180717134637_ResourceSeparation.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
6161
name: "CourseStudent",
6262
columns: table => new
6363
{
64+
Id = table.Column<int>(nullable: false)
65+
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn),
6466
courseid = table.Column<int>(name: "course_id", nullable: false),
6567
studentid = table.Column<int>(name: "student_id", nullable: false)
6668
},
@@ -82,14 +84,19 @@ protected override void Up(MigrationBuilder migrationBuilder)
8284
});
8385

8486
migrationBuilder.CreateIndex(
85-
name: "IX_Course_department-id",
87+
name: "IX_Course_department_id",
8688
table: "Course",
8789
column: "department_id");
8890

8991
migrationBuilder.CreateIndex(
90-
name: "IX_CourseStudent_student-id",
92+
name: "IX_CourseStudent_student_id",
9193
table: "CourseStudent",
9294
column: "student_id");
95+
96+
migrationBuilder.CreateIndex(
97+
name: "IX_CourseStudent_Id",
98+
table: "CourseStudent",
99+
column: "Id");
93100
}
94101

95102
protected override void Down(MigrationBuilder migrationBuilder)

src/Examples/JsonApiDotNetCoreExample/Models/Entities/CourseStudentEntity.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
using JsonApiDotNetCore.Models;
12
using Microsoft.EntityFrameworkCore.Infrastructure;
23
using System.ComponentModel.DataAnnotations.Schema;
34

45
namespace JsonApiDotNetCoreExample.Models.Entities
56
{
67
[Table("CourseStudent")]
7-
public class CourseStudentEntity
8+
public class CourseStudentEntity : Identifiable
89
{
910
private CourseEntity _course;
1011
private StudentEntity _student;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using JsonApiDotNetCore.Models;
2+
3+
namespace JsonApiDotNetCoreExample.Models.Resources
4+
{
5+
/// <summary>
6+
/// Note: EF Core *requires* the creation of an additional entity
7+
/// for many to many relationships and no longer implicitly creates
8+
/// it. While it may not make sense to create a corresponding "resource"
9+
/// for that relationship, due to the need to make the underlying
10+
/// framework and mapping understand the explicit navigation entity,
11+
/// a mirroring DTO resource is also required.
12+
/// </summary>
13+
public class CourseStudentResource : Identifiable
14+
{
15+
[HasOne("course")]
16+
public CourseResource Course { get; set; }
17+
public int CourseId { get; set; }
18+
19+
[HasOne("student")]
20+
public StudentResource Student { get; set; }
21+
public int StudentId { get; set; }
22+
}
23+
}

src/Examples/ResourceEntitySeparationExample/Startup.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ public virtual IServiceProvider ConfigureServices(IServiceCollection services)
3838
services.AddSingleton<ILoggerFactory>(loggerFactory);
3939

4040
services.AddDbContext<AppDbContext>(options => options
41-
.UseNpgsql(GetDbConnectionString())
42-
/*.UseLazyLoadingProxies()*/,
43-
ServiceLifetime.Transient);
41+
.UseNpgsql(GetDbConnectionString()),
42+
ServiceLifetime.Transient);
4443
services.AddScoped<IDbContextResolver, DbContextResolver<AppDbContext>>();
4544

4645
var mvcBuilder = services.AddMvcCore();

0 commit comments

Comments
 (0)