VB.NET이 있는 흐름 API
Code First를 사용하면 C# 또는 VB.NET 클래스를 사용해 모델을 정의할 수 있습니다. 클래스 및 속성의 특성을 사용하거나 흐름 API를 사용하여 필요에 따라 추가 구성을 수행할 수 있습니다. 이 연습에서는 VB.NET을 사용하여 흐름 API 구성을 수행하는 방법을 보여줍니다.
이 페이지에서는 Code First에 대한 기본적인 이해가 있다고 가정합니다. Code First에 대한 자세한 내용은 다음 연습을 확인하세요.
필수 구성 요소
이 연습을 완료하려면 Visual Studio 2010 또는 Visual Studio 2012 이상이 설치되어 있어야 합니다.
Visual Studio 2010을 사용하는 경우 NuGet도 설치해야 합니다.
애플리케이션 만들기
작업을 간단하게 유지하기 위해 Code First를 사용하여 데이터 액세스를 수행하는 기본 콘솔 애플리케이션을 빌드합니다.
- Visual Studio를 엽니다.
- 파일 -> 새로 만들기 -> 프로젝트
- 왼쪽 메뉴에서 Windows를 선택하고 콘솔 애플리케이션을 선택합니다.
- 이름으로 CodeFirstVBSample을 입력합니다.
- 확인을 선택합니다.
모델 정의
이 단계에서는 개념적 모델을 나타내는 VB.NET POCO 엔터티 형식을 정의합니다. 이 클래스는 기본 클래스에서 파생되거나 인터페이스를 구현할 필요가 없습니다.
- 프로젝트에 새 클래스를 추가하고 클래스 이름에 SchoolModel을 입력합니다.
- 새 클래스의 콘텐츠를 다음 코드로 바꿉니다.
Public Class Department
Public Sub New()
Me.Courses = New List(Of Course)()
End Sub
' Primary key
Public Property DepartmentID() As Integer
Public Property Name() As String
Public Property Budget() As Decimal
Public Property StartDate() As Date
Public Property Administrator() As Integer?
Public Overridable Property Courses() As ICollection(Of Course)
End Class
Public Class Course
Public Sub New()
Me.Instructors = New HashSet(Of Instructor)()
End Sub
' Primary key
Public Property CourseID() As Integer
Public Property Title() As String
Public Property Credits() As Integer
' Foreign key that does not follow the Code First convention.
' The fluent API will be used to configure DepartmentID_FK to be the foreign key for this entity.
Public Property DepartmentID_FK() As Integer
' Navigation properties
Public Overridable Property Department() As Department
Public Overridable Property Instructors() As ICollection(Of Instructor)
End Class
Public Class OnlineCourse
Inherits Course
Public Property URL() As String
End Class
Partial Public Class OnsiteCourse
Inherits Course
Public Sub New()
Details = New OnsiteCourseDetails()
End Sub
Public Property Details() As OnsiteCourseDetails
End Class
' Complex type
Public Class OnsiteCourseDetails
Public Property Time() As Date
Public Property Location() As String
Public Property Days() As String
End Class
Public Class Person
' Primary key
Public Property PersonID() As Integer
Public Property LastName() As String
Public Property FirstName() As String
End Class
Public Class Instructor
Inherits Person
Public Sub New()
Me.Courses = New List(Of Course)()
End Sub
Public Property HireDate() As Date
' Navigation properties
Private privateCourses As ICollection(Of Course)
Public Overridable Property Courses() As ICollection(Of Course)
Public Overridable Property OfficeAssignment() As OfficeAssignment
End Class
Public Class OfficeAssignment
' Primary key that does not follow the Code First convention.
' The HasKey method is used later to configure the primary key for the entity.
Public Property InstructorID() As Integer
Public Property Location() As String
Public Property Timestamp() As Byte()
' Navigation property
Public Overridable Property Instructor() As Instructor
End Class
파생 컨텍스트 정의
Entity Framework에서 형식을 사용하기 시작하므로 EntityFramework NuGet 패키지를 추가해야 합니다.
- **프로젝트 ->NuGet 패키지 관리...
참고 항목
NuGet 패키지 관리... 옵션이 없는 경우 최신 버전의 NuGet을 설치해야 합니다.
- 온라인 탭을 선택합니다.
- EntityFramework 패키지를 선택합니다.
- 설치를 클릭합니다.
이제 데이터베이스와의 세션을 나타내는 파생 컨텍스트를 정의하여 데이터를 쿼리하고 저장할 수 있습니다. System.Data.Entity.DbContext에서 파생되는 컨텍스트를 정의하고 모델의 각 클래스에 대해 유형이 지정된 DbSet<TEntity>를 공개합니다.
- 프로젝트에 새 클래스를 추가하고 클래스 이름에 SchoolContext를 입력합니다.
- 새 클래스의 콘텐츠를 다음 코드로 바꿉니다.
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure
Imports System.Data.Entity.ModelConfiguration.Conventions
Public Class SchoolContext
Inherits DbContext
Public Property OfficeAssignments() As DbSet(Of OfficeAssignment)
Public Property Instructors() As DbSet(Of Instructor)
Public Property Courses() As DbSet(Of Course)
Public Property Departments() As DbSet(Of Department)
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
End Sub
End Class
흐름 API를 사용하여 구성
이 섹션에서는 흐름 API를 사용하여 테이블 매핑에 대한 형식, 열 매핑에 대한 속성 및 모델의 tables\type 간 관계를 구성하는 방법을 보여줍니다. 흐름 API는 DbModelBuilder 형식을 통해 노출되며 DbContext에서 OnModelCreating 메서드를 재정의하여 가장 일반적으로 액세스됩니다.
- 다음 코드를 복사하여 SchoolContext 클래스에 정의된 OnModelCreating 메서드에 추가합니다. 주석은 각 매핑이 수행하는 작업을 설명합니다.
' Configure Code First to ignore PluralizingTableName convention
' If you keep this convention then the generated tables
' will have pluralized names.
modelBuilder.Conventions.Remove(Of PluralizingTableNameConvention)()
' Specifying that a Class is a Complex Type
' The model defined in this topic defines a type OnsiteCourseDetails.
' By convention, a type that has no primary key specified
' is treated as a complex type.
' There are some scenarios where Code First will not
' detect a complex type (for example, if you do have a property
' called ID, but you do not mean for it to be a primary key).
' In such cases, you would use the fluent API to
' explicitly specify that a type is a complex type.
modelBuilder.ComplexType(Of OnsiteCourseDetails)()
' Mapping a CLR Entity Type to a Specific Table in the Database.
' All properties of OfficeAssignment will be mapped
' to columns in a table called t_OfficeAssignment.
modelBuilder.Entity(Of OfficeAssignment)().ToTable("t_OfficeAssignment")
' Mapping the Table-Per-Hierarchy (TPH) Inheritance
' In the TPH mapping scenario, all types in an inheritance hierarchy
' are mapped to a single table.
' A discriminator column is used to identify the type of each row.
' When creating your model with Code First,
' TPH is the default strategy for the types that
' participate in the inheritance hierarchy.
' By default, the discriminator column is added
' to the table with the name “Discriminator”
' and the CLR type name of each type in the hierarchy
' is used for the discriminator values.
' You can modify the default behavior by using the fluent API.
modelBuilder.Entity(Of Person)().
Map(Of Person)(Function(t) t.Requires("Type").
HasValue("Person")).
Map(Of Instructor)(Function(t) t.Requires("Type").
HasValue("Instructor"))
' Mapping the Table-Per-Type (TPT) Inheritance
' In the TPT mapping scenario, all types are mapped to individual tables.
' Properties that belong solely to a base type or derived type are stored
' in a table that maps to that type. Tables that map to derived types
' also store a foreign key that joins the derived table with the base table.
modelBuilder.Entity(Of Course)().ToTable("Course")
modelBuilder.Entity(Of OnsiteCourse)().ToTable("OnsiteCourse")
modelBuilder.Entity(Of OnlineCourse)().ToTable("OnlineCourse")
' Configuring a Primary Key
' If your class defines a property whose name is “ID” or “Id”,
' or a class name followed by “ID” or “Id”,
' the Entity Framework treats this property as a primary key by convention.
' If your property name does not follow this pattern, use the HasKey method
' to configure the primary key for the entity.
modelBuilder.Entity(Of OfficeAssignment)().
HasKey(Function(t) t.InstructorID)
' Specifying the Maximum Length on a Property
' In the following example, the Name property
' should be no longer than 50 characters.
' If you make the value longer than 50 characters,
' you will get a DbEntityValidationException exception.
modelBuilder.Entity(Of Department)().Property(Function(t) t.Name).
HasMaxLength(60)
' Configuring the Property to be Required
' In the following example, the Name property is required.
' If you do not specify the Name,
' you will get a DbEntityValidationException exception.
' The database column used to store this property will be non-nullable.
modelBuilder.Entity(Of Department)().Property(Function(t) t.Name).
IsRequired()
' Switching off Identity for Numeric Primary Keys
' The following example sets the DepartmentID property to
' System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None to indicate that
' the value will not be generated by the database.
modelBuilder.Entity(Of Course)().Property(Function(t) t.CourseID).
HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
'Specifying NOT to Map a CLR Property to a Column in the Database
modelBuilder.Entity(Of Department)().
Ignore(Function(t) t.Administrator)
'Mapping a CLR Property to a Specific Column in the Database
modelBuilder.Entity(Of Department)().Property(Function(t) t.Budget).
HasColumnName("DepartmentBudget")
'Configuring the Data Type of a Database Column
modelBuilder.Entity(Of Department)().Property(Function(t) t.Name).
HasColumnType("varchar")
'Configuring Properties on a Complex Type
modelBuilder.Entity(Of OnsiteCourse)().Property(Function(t) t.Details.Days).
HasColumnName("Days")
modelBuilder.Entity(Of OnsiteCourse)().Property(Function(t) t.Details.Location).
HasColumnName("Location")
modelBuilder.Entity(Of OnsiteCourse)().Property(Function(t) t.Details.Time).
HasColumnName("Time")
' Map one-to-zero or one relationship
' The OfficeAssignment has the InstructorID
' property that is a primary key and a foreign key.
modelBuilder.Entity(Of OfficeAssignment)().
HasRequired(Function(t) t.Instructor).
WithOptional(Function(t) t.OfficeAssignment)
' Configuring a Many-to-Many Relationship
' The following code configures a many-to-many relationship
' between the Course and Instructor types.
' In the following example, the default Code First conventions
' are used to create a join table.
' As a result the CourseInstructor table is created with
' Course_CourseID and Instructor_InstructorID columns.
modelBuilder.Entity(Of Course)().
HasMany(Function(t) t.Instructors).
WithMany(Function(t) t.Courses)
' Configuring a Many-to-Many Relationship and specifying the names
' of the columns in the join table
' If you want to specify the join table name
' and the names of the columns in the table
' you need to do additional configuration by using the Map method.
' The following code generates the CourseInstructor
' table with CourseID and InstructorID columns.
modelBuilder.Entity(Of Course)().
HasMany(Function(t) t.Instructors).
WithMany(Function(t) t.Courses).
Map(Sub(m)
m.ToTable("CourseInstructor")
m.MapLeftKey("CourseID")
m.MapRightKey("InstructorID")
End Sub)
' Configuring a foreign key name that does not follow the Code First convention
' The foreign key property on the Course class is called DepartmentID_FK
' since that does not follow Code First conventions you need to explicitly specify
' that you want DepartmentID_FK to be the foreign key.
modelBuilder.Entity(Of Course)().
HasRequired(Function(t) t.Department).
WithMany(Function(t) t.Courses).
HasForeignKey(Function(t) t.DepartmentID_FK)
' Enabling Cascade Delete
' By default, if a foreign key on the dependent entity is not nullable,
' then Code First sets cascade delete on the relationship.
' If a foreign key on the dependent entity is nullable,
' Code First does not set cascade delete on the relationship,
' and when the principal is deleted the foreign key will be set to null.
' The following code configures cascade delete on the relationship.
' You can also remove the cascade delete conventions by using:
' modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()
' and modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>().
modelBuilder.Entity(Of Course)().
HasRequired(Function(t) t.Department).
WithMany(Function(t) t.Courses).
HasForeignKey(Function(d) d.DepartmentID_FK).
WillCascadeOnDelete(False)
모델 사용
SchoolContext를 통해 일부 데이터 액세스를 수행하여 작동 중인 모델을 확인해 보겠습니다.
- Main 함수가 정의된 Module1.vb 파일을 엽니다.
- 다음 Module1 정의를 복사하여 붙여넣습니다.
Imports System.Data.Entity
Module Module1
Sub Main()
Using context As New SchoolContext()
' Create and save a new Department.
Console.Write("Enter a name for a new Department: ")
Dim name = Console.ReadLine()
Dim department = New Department With { .Name = name, .StartDate = DateTime.Now }
context.Departments.Add(department)
context.SaveChanges()
' Display all Departments from the database ordered by name
Dim departments =
From d In context.Departments
Order By d.Name
Select d
Console.WriteLine("All Departments in the database:")
For Each department In departments
Console.WriteLine(department.Name)
Next
End Using
Console.WriteLine("Press any key to exit...")
Console.ReadKey()
End Sub
End Module
이제 애플리케이션을 실행하고 테스트할 수 있습니다.
Enter a name for a new Department: Computing
All Departments in the database:
Computing
Press any key to exit...
.NET