1 新建项目
开始执行不调试运行
2 打开管理NuGet程序包
3 依次安装Microsoft.EntityFrameworkCore、Microsoft.EntityFrameworkCore.SqlServer
4 查看依赖项
5 创建实体类
namespace NetFrameworkWeb.Models
{
public class User
{
public long id { get; set; }
public string? name { get; set; }
public string? pwd { get; set; }
public override string? ToString()
{
return id+" "+name+" "+pwd;
}
}
}
6 创建EntityFramework上下文
using Microsoft.EntityFrameworkCore;
namespace NetFrameworkWeb.Models
{
public partial class DataContext : DbContext
{
public DataContext() { }
public DataContext(DbContextOptions<DataContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
public DbSet<User> t_users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Data Source=.;Initial Catalog=TestDB;Persist Security Info=True;TrustServerCertificate=True;User ID=sa;Password=***********;")
.LogTo(Console.WriteLine, LogLevel.Information);
}
}
}