본문 바로가기
728x90
반응형

Entity Framework Core26

.NET Core MVC] EF Core - 5.복잡한 data model 생성 앞의 tutorial에서는 3 entities의 간단한 data model을 다루어보았다. 추가 entities와 관계를 설정하고 data model을 customize해보자. Customize the Data model attribute를 사용해서 data model을 customize해보자. DataType attribute 학생 등록일에 대해 날짜까지만 관리하지만, web page는 data와 time을 표시한다. annotation attribute를 사용하므로 web page에서 날짜까지만 표시하도록 수정할 수 있다. [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode .. 2023. 8. 7.
.NET Core MVC] EF Core - 4.Migrations Migrations 개발자가 새 application을 개발할 때, data model이 자주 변경된다. model을 변경할 수록 database와 sync가 맞지 않게된다. 이 튜토리얼을 시작단계에서, EF 설정을 하면서 없었던 database를 생성했다. 이후로, data model이 변경되면 database를 지우고 EF는 data model에 맞는 database를 생성하고 test data를 주입한다. 이 방법은, application을 production 상태로 배포하기 전까지 사용할 수 있다. production 상태의 서비스는 유지해야 하는 데이터를 저장하기 때문에, 새 컬럼을 추가할 때마다 모든 데이터를 날릴 수는 없다. EF Migration은 database를 지우고 다시 만드는 방법.. 2023. 8. 7.
.NET Core MVC] EF Core - 3.정렬, 검색, 페이징 StudentsController.cs public async Task Index(string sortOrder, string currentFilter, string searchString, int? pageNumber) { int pageSize = 3; if(searchString != null) { pageNumber = 1; } else { searchString = currentFilter; } ViewData["CurrentSort"] = sortOrder; ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewData["DateSortParm"] = sortOrder == "Date" ? "date.. 2023. 8. 1.
.NET Core MVC] EF Core - 2.CRUD 상세화면 public async Task Details(int? id) { if (id == null) { return NotFound(); } // null-forgiving operator(!)를 사용해서 compiler에러를 없앤다. // !를 너무 자주 사용해야 한다면, non-nullable로 바꾸고 Fluent API 또는 Data Annotation을 통해서 optional 구성할 수 있다. var student = await _context.Students .Include(s => s.Enrollments!) .ThenInclude(e => e.Course) .AsNoTracking() .FirstOrDefaultAsync(m => m.ID == id); if (student == null.. 2023. 7. 31.
.NET Core MVC] EF Core - 1.기본설정(Model, DbContext) MS Tutorial을 따라해보았다~ Contoso University web app을 만들어보자. 개발환경 Visual Studio Code MS SQL Server Express : 설치방법은 아래 링크 참조 https://bigexecution.tistory.com/252 .NET 7 SDK 설치 : 설치파일 링크는 아래에 https://dotnet.microsoft.com/en-us/download/dotnet/7.0 EF Core Nuget Packages 설치 Microsoft.EntityFrameworkCore : EF Core basic functionality Microsoft.EntityFrameworkCore.Relational : Common relational database fu.. 2023. 7. 27.
Entity Framework Core] Reverse engineering (scaffolding) 예시 명령어는 .NET CLI 기준으로 작성했다. Database Schemas 관리 EF Core는 EF Core model과 database schema 사이에 sync를 유지하기 위해, 2가지 방법을 제공한다. 둘 중 선택하려면, EF Core model과 database schema 중 기준이 되는 것을 정해야 한다. EF Core model이 기준이라면, Migrations을 사용해야 한다. EF Core model을 수정하면서 database에도 동일하게 적용해야 한다. database schema가 기준이라면, Reverse Engineering을 사용한다. Reverse engineering database schema 기준으로 entity type classes와 DbContext cla.. 2023. 7. 25.
728x90
반응형