Net Core Page 第一步:数据库连接设置的基本步骤

数据库连接字符串

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(options => {
                options.UseSqlServer(Configuration.GetConnectionString("MyConnection"));
            });

 >DlContext.cs 这是一个类

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

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

        
    }

  

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

3、页面引用

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

        }

  

转载于:https://www.cnblogs.com/ccxcn/p/11226064.html

你可能感兴趣的:(数据库)