如何将Razor Pages ASP.NET应用程序连接到已经存在的已存在的MSSQL数据库?

发布于 2025-02-04 01:05:56 字数 111 浏览 2 评论 0原文

我想创建一个Razor Pages应用程序,在其中我将显示数据库中的条目。我应该在连接字符串中放置什么?我可以登录到此数据库和服务器。

在连接到它之后,如何访问它并简单地显示页面中DB的所有行?

I want to create a Razor Pages app where I would display the entries from a database. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如歌彻婉言 2025-02-11 01:05:56

您应该考虑一些事情,以便在剃须刀页面中访问DB的实体。

  1. 中,将连接字符串设置

在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. 添加DB上下文类 为继承DBContext Ex。 “ 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. 将DBContext添加到应用程序服务中
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<YourDBContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}

,您可以通过上下文中使用依赖项注入和访问实体:

public class IndexModel : PageModel
    {
        YourDBContext_context;

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

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

There are a few things you should consider in order to access entities from DB in Razor Pages.

  1. Set the Connection String in 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. "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
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();
  ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文