상세 컨텐츠

본문 제목

[.NET Core] ASP.NET Core EF(Entity Framework) Core에서 DB 컬럼 기본값 지정

Programing/.NET Core

by 호짱 HoZang 2020. 10. 26. 20:58

본문

[.NET Core] ASP.NET Core EF(Entity Framework) Core에서 DB 컬럼 기본값 지정

 

1. 사용중인 DbContext 파일에 추가

namespace ProjectName.Data
{
    public class DbContext : IdentityDbContext<HomepageUser>
    {
        public DbContext(DbContextOptions<DbContext> options)
            : base(options)
        {
        }
        public DbSet<ProjectName.Models.Article> Article { get; set; }
        .
        .
        .
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            EntityTypeBuilder<Article> article = builder.Entity<Article>();
            article.Property(m => m.Content)
                .HasDefaultValue("");
            article.Property(m => m.CategoryId)
                .HasDefaultValue(1);
            article.Property(m => m.SignDate)
                .HasDefaultValueSql("CURRENT_TIMESTAMP()");
            article.Property(m => m.EditDate)
                .HasDefaultValueSql("CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP()");

            EntityTypeBuilder<CustomUser> customUser = builder.Entity<CustomUser>();
            customUser.Property(m => m.Name)
                .HasDefaultValue("");
        }
    }
}

 

 

 

 

관련글 더보기

댓글 영역