EntityFrameworkCore 6 μμ λ€λλ€ κ΄κ³ ꡬμ±μ΄ ν₯μλμλ€κ³ ν΄μ κ΄λ ¨ λ΄μ©μ νμΈνμ΅λλ€.
λ°μ΄ν° μ μΆλ ₯μ μ€κ° ν μ΄λΈμ κ±°μΉμ§ μκ³ κ΅¬νμ΄ κ°λ₯ν©λλ€.
GitHub: bbonkr/sample.ef.mtom μ μ₯μμμ μ½λλ₯Ό νμΈν μ μμ΅λλ€.
μμ ν μ΄λΈ
Student
Name | Nullable | Constaint |
---|---|---|
Id | NN | PK |
Name | NN |
Cource
Name | Nullable | Constaint |
---|---|---|
Id | NN | PK |
Title | NN |
Enrollment
Name | Nullable | Constaint |
---|---|---|
StudentId | NN | PK |
CourseId | NN | PK |
μν°ν° νμ ꡬμ±
.net core 3.1
features/net3 λΈλμΉμμ κ΄λ ¨ μ½λλ₯Ό νμΈν μ μμ΅λλ€.
public class EnrollmentEntityTypeConfiguration : IEntityTypeConfiguration<Enrollment>
{
public void Configure(EntityTypeBuilder<Enrollment> builder)
{
builder.HasKey(x => new { x.StudentId, x.CourseId });
builder.Property(x => x.StudentId)
.IsRequired();
builder.Property(x => x.CourseId)
.IsRequired();
builder.HasOne(x => x.Student)
.WithMany(x => x.Enrollments)
.HasForeignKey(x => x.StudentId);
builder.HasOne(x => x.Course)
.WithMany(x => x.Enrollments)
.HasForeignKey(x => x.CourseId);
}
}
.NET 5
features/net5 λΈλμΉμμ κ΄λ ¨ μ½λλ₯Ό νμΈν μ μμ΅λλ€.
.net core 3.1 ꡬμ±μμ λ³κ²½μ¬νμ΄ μμ΅λλ€.
.NET 6
EnrollmentEntityTypeConfiguration ν΄λμ€λ₯Ό μ κ±°νκ³ , CourseEntityTypeConfiguration ν΄λμ€μμ λ€λλ€ κ΄κ³λ₯Ό μ€μ ν©λλ€.
features/net6 λΈλμΉ νΉμ main λΈλμΉμμ κ΄λ ¨ μ½λλ₯Ό νμΈν μ μμ΅λλ€.
public class CourseEntityTypeConfiguration : IEntityTypeConfiguration<Course>
{
public void Configure(EntityTypeBuilder<Course> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.IsRequired()
.ValueGeneratedOnAdd();
builder.Property(x => x.Title)
.IsRequired();
builder.HasMany(x => x.Students)
.WithMany(x => x.Courses)
.UsingEntity<Enrollment>(
j => j.HasOne(x => x.Student).WithMany(x => x.Enrollments).HasForeignKey(x => x.StudentId),
j => j.HasOne(x => x.Course).WithMany(x => x.Enrollments).HasForeignKey(x => x.CourseId),
j =>
{
j.HasKey(x => new { x.StudentId, x.CourseId });
});
}
}
μν°ν° νμ ꡬμ±μ λ³κ²½ν ν λ§μ΄κ·Έλ μ΄μ μ½λλ₯Ό μμ±ν΄λ λ³κ²½μ¬νμ΄ μμ΅λλ€. μν°ν° νμ ꡬμ±μ λ³κ²½ν ν μμ±λ λ§μ΄κ·Έλ μ΄μ μ½λ: 20211218051811_Change mtom
μ¬μ©
.net core 3.1
νμμ μμ μ 보λ₯Ό μ λ ₯ν©λλ€.
var enrollments = Context.Courses
.ToList()
.Where((_, index) => index < value)
.Select(x => new Enrollment
{
CourseId = x.Id,
});
student.Enrollments
.AddRange(enrollments);
νμ μ 보μ νμμ μμ μ 쿼리ν©λλ€.
var students = await Context.Students
.Include(x => x.Enrollments)
.Select(student => new
{
Name = student.Name,
Courses = x.Enrollments.Select(enrollment => new
{
Title = enrollment.Course.Title,
}),
});
.NET 5
λ³κ²½μ¬νμ΄ μμ΅λλ€.
.NET 6
νμμ μμ μ 보λ₯Ό μ λ ₯ν©λλ€.
Enrollments λ₯Ό μ¬μ©ν΄μ μ λ ₯νμ§ μκ³ , Courses μ¬μ©ν΄μ μ λ ₯ν μ μμ΅λλ€.
var coursesToEnroll = Context.Courses
.ToList()
.Where((_, index) => index < value);
student.Courses.AddRange(coursesToEnroll);
νμ μ 보μ μμ μ 보λ₯Ό 쿼리ν©λλ€.
Enrollments λ₯Ό μ¬μ©νμ§ μκ³ , Courses λ₯Ό 쿼리ν μ μμ΄ μ§κ΄μ μΌλ‘ μ¬μ©ν μ μλ€κ³ μκ°λ©λλ€.
var students = await Context.Students
.Include(x => x.Courses)
.Select(student => new
{
Name = student.Name,
Courses = student.Courses.Select(course => new
{
Title = course.Title,
}),
})
κ·Έ μΈ
dotnet-ef tool
dotnet CLI λ₯Ό μ¬μ©ν΄μ dotnet λꡬλ₯Ό ν΄λΉ νλ‘μ νΈ λ²μμμ κ΄λ¦¬νλ €λ©΄, 맀λνμ€νΈ νμΌμ λ¨Όμ μμ±ν΄μΌ ν©λλ€.
$ dotnet new tool-manifest
μ°Έμ‘°: nuget: dotnet-ef versions tab.
.net core 3.1
$ dotnet tool install --local dotnet-ef --version 3.1.22
.net 5
$ dotnet tool update --local dotnet-ef --version 5.0.13
.net 6
$ dotnet tool update --local dotnet-ef --version 6.0.1
λ§μ΄κ·Έλ μ΄μ μ½λ μμ±
Sample.App
νλ‘μ νΈλ₯Ό μμνλ‘μ νΈλ‘ μ€μ νκ³ , AppDbContext λ₯Ό λμμΌλ‘ Sample.Data.SqlServer
νλ‘μ νΈμ λ§μ΄κ·Έλ μ΄μ
μ½λλ₯Ό μμ±ν©λλ€.
$ cd src/Sample.Data
$ dotnet ef migrations add "Migrations name" --context AppDbContext --startup-project ../Sample.App --project ../Sample.Data.SqlServer
λ§μ΄κ·Έλ μ΄μ μ½λλ₯Ό λ€λ₯Έ νλ‘μ νΈμμ κ΄λ¦¬ν μ μμ΅λλ€.
κ²°λ‘
EFCore 6 μμ λλμ΄ λ€λλ€ κ΄κ³μ ꡬνμ΄ μ 리λμ΄, λ¬Έμ μμ΄ μνλ λͺ¨μμΌλ‘ μ€μ ν μ μμμ΅λλ€.
κ·Έλ¦¬κ³ , λ°μ΄ν° μ μΆλ ₯λ μ§κ΄μ μΈ λ°©μμΌλ‘ ꡬνμ΄ κ°λ₯ν΄μ‘μ΅λλ€.