数据库连接字符串

1、appsettings.json

"ConnectionStrings": {
    "MyConnection": "server=.\\ms2008r2;database=sql_huiim;trusted_connection=true;"

//"MyConnection": "server=.\\ms2008r2;database=sql_huiim;User Id=sa; Password=123123; Pooling=true;"
  }

2、Startup.cs()

services.AddDbContext<DlContext>(options => {
                options.UseSqlServer(Configuration.GetConnectionString("MyConnection"));
            });

 >DlContext.cs 这是一个类

public class DlContext:DbContext
    {
        public DlContext(DbContextOptions<DlContext> options) : base(options)
        {

        }
        public DbSet<Im_User> Im_User { get; set; }
        public DbSet<Im_Msg> Im_Msg { get; set; }

        
    }

  

 这里的命名如果不一样,就会报错,需要注册一下。

3、页面引用

private readonly DlContext dl;
 public List<Im_User> Userlist = new List<Im_User>();
public IndexModel(DlContext _dl)
        {
            dl = _dl;
        }
public void OnGet()
        {
            var ns = dl.Im_User.ToList();
            Userlist = ns;

        }