注释型实体映射属性

[Table("USER_T")] //指点表名称
[Column("blog_id")] //列名
[Column(TypeName = "varchar(200)")]
[Column(TypeName = "decimal(5, 2)")] //数据类型
[MaxLength(500)] //字符串最大长度,默认类型为nvarchar(500)
[Precision(3)]  //可定义DateTime类型
[Precision(14, 2)]   //精度和小数位

约束
[Required] //字符串非空

[Key] //主键
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]  //设置自增

public Datetime? CreatedTime{get;set;} //?代表时间格式可以为空

public bool CacheDbResults { get; set; } = true; //设置默认值

[NotMapped] //标识不映射到数据库字段

还可以通过Fluent API方式显示设置实体映射属性

注册数据库连接方式一,构造函数创建DbContext。依赖注入

1.在appsettings.json中添加连接字符串
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "ctwContext": "Data Source=ctw.db"
  }
}

 2.在启动文件中注册

// Add services to the container.
builder.Services.AddRazorPages();

//builder.Services.AddDbContext<MyContext>();
builder.Services.AddDbContext<MyContext>(options =>
  options.UseSqlite(builder.Configuration.GetConnectionString("ctwContext")));

3.创建继承DbContext

 public class MyContext : DbContext
    {
        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    optionsBuilder.UseSqlite("Data Source=ctw.db");
        //}
        public MyContext(DbContextOptions<MyContext> options)
            : base(options)
        {
        }
        public DbSet<User>? Users { get; set; }       

    }

4.通过构造函数创建DbContext

public class IndexModel : PageModel
    {
        private readonly Models.MyContext _context;
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger, Models.MyContext context)
        {
            _logger = logger;
            _context = context;
        }

        public void OnGet()
        {

        }
    }
注册数据库连接方式二,new新建DbContext。连接多数据库情况
1.创建继承DbContext
public class MyContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=ctw.db");
        }
        //public MyContext(DbContextOptions<MyContext> options)
        //    : base(options)
        //{
        //}
        public DbSet<User>? Users { get; set; }       

    }

2.在启动文件中注册

// Add services to the container.
builder.Services.AddRazorPages();

builder.Services.AddDbContext<MyContext>();
//builder.Services.AddDbContext<MyContext>(options =>
//  options.UseSqlite(builder.Configuration.GetConnectionString("ctwContext")));

var app = builder.Build();

3.new调用

public class IndexModel : PageModel
    {
        //private readonly Models.MyContext _context;
        private readonly ILogger<IndexModel> _logger;

        //public IndexModel(ILogger<IndexModel> logger, Models.MyContext context)
        //{
        //    _logger = logger;
        //    _context = context;
        //}
        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {
            using var context = new Models.MyContext();
            IQueryable<Models.User> blogs = context.Users;
        }
    }

 启动ef core数据迁移

1.先安装Microsoft.EntityFrameworkCore.Tools

安装包时注意版本一致!并不是所有包都要是最新

tools包含的全部命令
Add-Migration
Bundle-Migration
Drop-Database
Get-DbContext
Get-Migration
Optimize-DbContext
Remove-Migration
Scaffold-DbContext
Script-Migration
Update-Database

2.Add-Migration xx //添加迁移,xx代表迁移名称

3.Update-Database //更新到数据库


(代码迁移)如果要删除某迁移类文件,最好先将数据库回滚到此迁移的上一版本,再删除
update-database -TargetMigration: [name] 命令,其中 [name] 要使用带时间戳的全名 

 

posted on 2022-04-25 11:25  雾夜飞虫  阅读(720)  评论(0编辑  收藏  举报