harxのブログ

プログラミングとかDTMとか写真とか

ASP.NET Entity Framework - モデル作成

モデルとなるクラスの作成

using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

namespace API.Models.Users
{
    [Index(nameof(Email), IsUnique = true)]
    public class User
    {
        [Key]
        public Guid Guid { get; set; }
        public string Email { get; set; }
        public string PasswordHash { get; set; }
        public string Name { get; set; }
        public DateTime CreateDateTime { get; set; }
        public DateTime LoginDataTime { get; set; }
        public bool IsAuthenticated { get; set; }
    }
}

フォーム用のモデルクラスの作成

フォームではGuidを入力することがないのでフォーム用のクラスのプロパティには指定しない。 また、DBには登録されない。

namespace API.Models.Users
{
    public class CreateUserReq
    {
        public string Email { get; set; }
        public string PaswordHash { get; set; }
        public string Name { get; set; }
    }
}