1.创建一个APS.NET MVC项目
2.安装Nuget包
Microsoft.EntityFrameworkCore.Design Microsoft.EntityFrameworkCore.SqlServer
3.在Models中添加Book实体类
public class Book { /// <summary> /// id /// </summary> public int Id { get; set; } /// <summary> /// 书本名称 /// </summary> public string Name { get; set; } /// <summary> /// 价格 /// </summary> public int Price { get; set; } /// <summary> /// 所属类型 /// </summary> public string Type { get; set; } }
4.在appsettings.json中添加链接数据库链接
"ConnectionStrings": { "connStr": "server=.;database=BingDbContext;uid=sa;pwd=123456;TrustServerCertificate=True;" }
5.新建一个类,我这点取名BingBDContext.cs
public class BingBDContext:DbContext { public BingBDContext(DbContextOptions<BingBDContext> options) : base(options) { } //相当于 OnConfiguring 方法 //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) //{ // optionsBuilder.UseSqlServer("链接数据库链接"); //} public DbSet<Book> Books { get; set; } public DbSet<Post> Posts { get; set; } }
6.在Program.cs中注入BingBDContext
builder.Services.AddDbContext<BingBDContext>(p => { p.UseSqlServer(builder.Configuration.GetConnectionString("connStr")); });
7. 打开解决方案文件,cmd进入,执行
dotnet ef migrations add 自己取一个名字 dotnet ef database update
dotent 需要安装运行时,
dotnet ef 也需要安装
可查看 https://i.cnblogs.com/posts/edit;postId=18283747 安装dotnet运行时,和dotnet ef
8.在HomeController.cs中使用
标签:set,get,APS,ef,BingBDContext,MVC,dotnet,NET,public From: https://www.cnblogs.com/tlfe/p/18284025