简体   繁体   English

如何将 Razor Pages ASP.NET 应用程序连接到现有的非本地 MSSQL 数据库?

[英]How do I connect a Razor Pages ASP.NET app to an already existing, not local MSSQL database?

I want to create a Razor Pages app where I would display the entries from a database.我想创建一个 Razor Pages 应用程序,在其中显示数据库中的条目。 What am I supposed to put in the Connection String?我应该在连接字符串中放入什么? I have the login to this database and the server.我有这个数据库和服务器的登录名。

And after I connect to it, how do I access it and simply display all the rows from the db in a page?在我连接到它之后,我如何访问它并简单地在页面中显示数据库中的所有行?

There are a few things you should consider in order to access entities from DB in Razor Pages.为了从 Razor Pages 中的 DB 访问实体,您应该考虑一些事项。

  1. Set the Connection String in appsettings.json在 appsettings.json 中设置连接字符串

local:当地的:

"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=XXXXXXX;Trusted_Connection=True;"
  },

remote:偏僻的:

"ConnectionStrings": {
    "DefaultConnection": "Server=xxx.xxx.xxx.xx;Database=XXXXXXX;User Id=yourUsername;password=yourPassword;Trusted_Connection=False;MultipleActiveResultSets=true;"
  }
  1. Add DB Context class that inherits DBContext ex.添加继承 DBContext ex 的 DB Context 类。 "YourDBContext" “你的数据库上下文”
public class YourDBContext: DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    }
}
  1. Add DbContext to app services将 DbContext 添加到应用服务
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<YourDBContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}

Than you can use Dependency Injection and access entity in your page via context:您可以通过上下文在页面中使用依赖注入和访问实体:

public class IndexModel : PageModel
    {
        YourDBContext_context;

        public IndexModel(YourDBContext context)
        {
            _context = context;
        }

 public async Task<IActionResult> OnPostAsync()
        {
            IList<Blog> entities = await _context.Blogs.ToListAsync();
  ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何连接到ASP.NET MVC中的现有数据库? - How do I connect to an existing database in ASP.NET MVC? 将 ASP.NET Core Identity db 与 Razor Pages 应用程序中已创建的数据库集成 - Integrating ASP.NET Core Identity db with an already created database in a Razor Pages app 如何在本地服务器上运行已经存在的asp.net mvc应用程序 - How do I run an already existing asp.net mvc application om my local server 如何连接到本地MSSQL数据库 - How do I connect to a Local MSSQL Database 如何将ASP.NET Core 2.2 Razor页面与MySql数据库连接? - How to connect ASP.NET Core 2.2 Razor pages with MySql database? 如何将AWS ASP.NET Core日志记录添加到ASP.NET Core 2.0 Razor Pages应用程序? - How do you add AWS ASP.NET Core Logging to an ASP.NET Core 2.0 Razor Pages app? 如何将Razor组件集成到具有多个路由的现有Razor Pages Asp.net Core项目中 - How to integrate Razor Components into existing Razor Pages Asp.net Core project with multiple routes 如何从ASP.Net网页和剃须刀上使用MongoDB? - How do I work with MongoDB from ASP.Net web pages and razor? 如何在ASP.Net Core 2.0 Razor页面中发布IFormFile? - How do I post an IFormFile in ASP.Net Core 2.0 Razor Pages? 如何在尝试使用 Razor Pages 删除 ASP.NET Core 中的记录时显示确认消息 - How do I display a confirmation message with trying to delete a record in ASP.NET Core using Razor Pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM