2.在类库项目中 添加实体模型和数据上下文
1 public class TopBaseModel 2 { 3 private Guid _id; 4 public Guid ID { 5 get { 6 if (_id == Guid.Empty) { 7 _id = Guid.NewGuid(); 8 } 9 return _id; 10 } 11 set { 12 _id = value; 13 } 14 } 15 }
1 using System.ComponentModel.DataAnnotations; 2 public class BaseModel:TopBaseModel 3 { 4 [Display(Name ="创建时间")] 5 public DateTime? CreateTime { get; set; } 6 [Display(Name ="创建人")] 7 [StringLength(50, ErrorMessage = "{0}最多输入{1}个字符")] 8 public string CreateBy { get; set; } 9 [Display(Name = "修改时间")] 10 public DateTime? UpdateTime { get; set; } 11 [Display(Name = "修改人")] 12 [StringLength(50, ErrorMessage = "{0}最多输入{1}个字符")] 13 public string UpdateBy { get; set; } 14 }
Frame_User实体模型,继承BaseModel
1 using System.ComponentModel.DataAnnotations; 2 public class Frame_User:BaseModel 3 { 4 [Display(Name ="用户")] 5 [StringLength(50, ErrorMessage = "{0}最多输入{1}个字符")] 6 public string UserName { get; set; } 7 [Display(Name = "角色名")] 8 [StringLength(50, ErrorMessage = "{0}最多输入{1}个字符")] 9 public string RoleName { get; set; } 10 }
ERPFrameDBContext数据上下文
1 public class ERPFrameDBContext : DbContext 2 { 3 public ERPFrameDBContext(DbContextOptions<ERPFrameDBContext> options) : base(options) 4 { 5 } 6 7 public DbSet<Frame_User> Frame_User { get; set; } 8 }
3. 项目文件配置
(1)“appsettings.json”添加配置项,数据库连接字符串
1 "ConnectionStrings": { 2 "todoContext": "Server=.;DataBase=ERPFrame;uid=sa;pwd=123456" 3 },
services.AddDbContext<ERPFrameDBContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("todoContext")));
如上所有配置完成后,重新编译成功后,在控制台进行数据库迁移 :
原文:https://www.cnblogs.com/ywkcode/p/11298752.html