目标

1 建立用户实体类UserInfo

2 创建表UserInfos

3 分析用户表与其他表可能的关系

功能分析

结构化描述:

用户表={编号,用户名,密码,QQ,性别,邮箱,地址,手机号,微信号,微信授权绑定OpenId,所在公司名称}

UserInfos={Id,UserName,Pwd,QQ,Gender,Email,Address,Mobile,WxCode,WxOpenId,CompanyName}

也可以这样

用户记录=编号+用户名+密码+QQ+性别+邮箱+地址+手机号+微信号+微信授权绑定OpenId+所在公司名称

UserInfo=Id+UserName+Pwd+QQ+Gender+Email+Address+Mobile+WxCode+WxOpenId+CompanyName

[Id]=[int+自增长+x>=1]

[UserName]=[字符串+长度至少3位+表内唯一或者同一类角色的用户名唯一]

[Email]=[符合email格式+包含@]

[Gender]=[tinyint+x>=0]+(0=未知|1=男|2=女,默认0)

预期效果图

 

代码

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace Xwy.Domain.Entities
{
    public class UserInfo
    {
        public int Id { get; set; }
        [Required]
        [MaxLength(200)]     
        public string UserName { get; set; }
        [Required]
        [MaxLength(200)]
        public string Pwd { get; set; }
        //[DefaultValue(0)] //默认值为0,codeFirst没有效果,只能通过fluentApi设置默认值
        public byte? Gender { get; set; }   //一般情况下要设置默认值,最好定义不为空的类型即byte

        //[Required]
        [MaxLength(200)]
        public string Mobile { get; set; }

        //[Required]
        [MaxLength(200)]
        public string Email { get; set; }

        public bool? IsAdmin { get; set; }

        //[Required]
        [Column("FirstName")]       
        //[Display(Name = "First Name")] //Display无法写进数据库字段说明,只能代码中显示用
        [StringLength(50)]
        public string FirstMidName { get; set; }

        [StringLength(50)]
        public string LastName { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]      
        public DateTime? HireDate { get; set; }   //加个问号成为可空类型

        //[Display(Name = "Full Name")]
        public string FullName
        {
            get { return LastName + ", " + FirstMidName; }   //只有get不会映射到数据库
        }

        [NotMapped]
        public int EmployeeNum { get; set; }

    }
}
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using Xwy.Domain.Entities;

namespace Xwy.Domain.DbContexts
{
    public class VueShopDbContext:DbContext
    {
        public VueShopDbContext()
        {

        }
        public VueShopDbContext(DbContextOptions<VueShopDbContext> options):base(options)
        {

        }
        // 放入可以使用的数据库对象
        public DbSet<TestRecord> TestRecords { get; set; }
        public DbSet<UserInfo> UserInfos { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Data Source=.;database=VueShopDb;uid=sa;pwd=123456");
                //optionsBuilder.UseSqlServer("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=.");//数据库连接字符串,其中TestDB是数据库名称
            }
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TestRecord>().ToTable("TestRecords","xwy");
            modelBuilder.Entity<UserInfo>().Property(x => x.Gender).HasDefaultValue((byte)0);//设置UserInfo的Gender不为空默认值为0,不转byte会报错
            modelBuilder.Entity<UserInfo>().Property(x => x.IsAdmin).HasDefaultValue(true);//设置UserInfo的IsAdmin不为空默认值为True
            modelBuilder.Entity<UserInfo>().HasIndex(x=>x.UserName).IsUnique();//设置UserInfo的UserName是唯一的约束
            base.OnModelCreating(modelBuilder);
        }
    }
}