ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [EFCore] 시작하기
    C#/EFCore 2022. 11. 9. 17:29

     


    Nuget_Package 설치

    Microsoft.EntityFrameworkCore.SqlServer
    Microsoft.EntityFrameworkCore.Design
    Microsoft.EntityFrameworkCore.Tools


    Employee.cs

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EFTest.Models
    {
        [Table("Employee")] //테이블 이름 정의 가능 없으면 class이름으로 자동생성
        public class Employee
        {
            [Key] // 키값 지정 
            public int Id { get; set; } // key
            public string Name { get; set; }
            public DateTime DOB { get; set; }
            public decimal Salary { get; set; }
        }
    }
        [Table("TagData")]
        public class TagData
        {
            [Key]
            // Id 지정해서 안넣어도 만들 때 자동으로 유일키 생성
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]	
            public string Id { get; set; }
            [Required]
            public string Name { get; set; }
            
            public string Address { get; set; } = null;
            public string Type { get; set; } = null;
            public string Comment { get; set; } = null;
        }

    // Id 지정해서 안넣어도 만들 때 자동으로 유일키 생성
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public string Id { get; set; }


    Composite PrimaryKey


    MyDbContext.cs

    PM> Add-Migration mig1 //mig1이라는 마이그레이션 추가.
    
    (To undo this action, use Remove-Migration.)
    
    Remove-Migration // 이전 마이그레이션 삭제.
    
    PM> Update-Database // 마이그레이션 데이터베이스에 올리기.

     

    MyDb데이터베이스 안에 Employee테이블 생성.


    Insert

     

    Program.cs
    Data Insert


     

    728x90

    'C# > EFCore' 카테고리의 다른 글

    [EFCore] reset sqlite_sequence  (0) 2023.10.12
    [EFCore] Key 값 넣을 때  (1) 2023.10.06
    [EFCore] Grpc Data Parsing  (0) 2023.09.21
    [EFCore] SQLite .db 사용시 Close  (0) 2023.08.29

    댓글

Designed by Tistory.