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 options)

: base(options)

{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)

{

}

public DbSet 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);

}

}

}

7 在HomeController中的 public IActionResult Index()方法中测试

8 转到Index视图进行修改

9  重新生成解决方案并运行

好文推荐

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。