安包
依赖包:
ef core(core版本的ef)
ef sqlserver(数据库,也可使用其他数据库比如ef mysql等)
ef tool(数据迁移工具)
书写DbContext
创建DbContext
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
// 表,有多少个数据模型就有多少张表,就得添加多少个DbSet
public DbSet<Person> Persons { get; set; }
}
创建数据模型
public class Person
{
[Column(TypeName = "uniqueIdentifier")] // 表示这个是唯一的
public Guid PKUser { get; set; }
[Key]
public int UserID { get; set; }
public string UserName { get; set; }
}
向应用注入上下文
修改Startup.cs
,
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(con