简体   繁体   English

如何将ASP.Net Core MVC2项目连接到远程数据库

[英]How to connect an ASP.Net Core MVC2 project to a remote database

After developing ASP.Net web applications for as long as I care to remember, I've finally got some time to teach myself ASP.Net core MVC2 with EF (I'm using VS 2017 by the way). 在我记起开发ASP.Net Web应用程序的时间之后,我终于有时间自学使用EF的ASP.Net核心MVC2(顺便说一句,我使用的是VS 2017)。 I've got a fair number of databases sitting on a remote server and I would like to get a project to connect to one of them, and this is where I run into troubles. 我有很多数据库都位于远程服务器上,我想获得一个项目来连接其中的一个,这就是我遇到麻烦的地方。 There's a few good tutorials about connecting to an existing database, but they all connect to a local database. 关于连接到现有数据库,有一些很好的教程,但是它们都连接到本地数据库。
I'm probably being stupid and not seeing the obvious, but I could use some help on this. 我可能很愚蠢,看不到明显的东西,但是我可以为此提供一些帮助。 Does anyone know of a good tutorial that will teach me how to connect my project to a remote database? 有谁知道一个好的教程,可以教我如何将项目连接到远程数据库?
It used to be so easy in C# desktop or ASP.Net web apps with entity framework, I just built the database, connected it to my project, and away it went. 在带有实体框架的C#桌面或ASP.Net Web应用程序中,它曾经是如此的容易,我只是构建数据库,将其连接到我的项目,然后就消失了。 I want to connect an ASP.Net Core MVC app to a remote database using EF core. 我想使用EF core将ASP.Net Core MVC应用程序连接到远程数据库。

I'm sorry, after reading the comments I should have put in what code was failing and where. 很抱歉,在阅读注释后,我应该输入失败的代码和失败的位置。 Here it is now. 现在在这里。 (please excuse me, I've been banging my head on this for for over a week now and I'm getting frustrated). (请原谅,我已经为此努力了一个多星期,而我对此感到沮丧)。 Here's my Startup.cs file, with the configure services method I use to call the database: 这是我的Startup.cs文件,其中包含用于调用数据库的configure services方法:

public void ConfigureServices(IServiceCollection services) 
{
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration["Data:ConnStringName:ConnectionString"]));
            services.AddTransient<IProductRepository, EFProductRepository>();
            services.AddMvc();
        }

and here's the contents of my App.JSON file which sets the connection string: 这是设置连接字符串的App.JSON文件的内容:

{
  "Data": {
    "ConnStringName": {
      "ConnectionString": "Server= (Name of my remote server)\\SqlExpress;Database=databaseName;id=UserName;password=password:Trusted_Connection=True;MultipleActiveResultsSets=true"
    }
  }
}

the question is broad/no error message , but from what i understood , try to implement the below in your startup file (fill in your connection string details below) 问题是广泛/无错误消息,但据我了解,请尝试在启动文件中实现以下内容(在下面填写您的连接字符串详细信息)

     public void Configure(IApplicationBuilder app)
     {
        app.UseDataEngineProviders().AddDataEngine("DB Name", @"Data Source=;Provider=;Initial Catalog=", "table name");

        app.UseStaticFiles();
        app.UseMvc();
     }

there are also many data sourse types available from the method UseDataEngineProviders that you can use . 您还可以使用UseDataEngineProviders方法提供许多数据源类型。

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

相关问题 如何将 ASP.NET Core 5 MVC 项目连接到 Supabase 数据库而不是 SQL 服务器? - How to connect ASP.NET Core 5 MVC project to Supabase database instead of SQL Server? 如何将plupload集成到asp.net MVC2项目中 - How to integated plupload into asp.net MVC2 project 使用ASP.NET MVC2和jQuery进行远程验证 - Remote Validation With ASP.NET MVC2 and jQuery 如何在 ubuntu 桌面上使用 Entity Framework Core 将 asp.net core MVC 项目连接到本地数据库 - how to connect asp.net core MVC project to local DB using Entity Framework Core on ubuntu desktop ASP.NET MVC2项目的DDD体系结构 - DDD Architecture for ASP.NET MVC2 Project 具有Oracle数据库的ASP.NET MVC2 Web应用程序 - ASP.NET MVC2 Web Application with Oracle Database 如何在 Asp.Net Core Mvc 5.0 中将 sql 数据库与 ado.net 连接? - How to connect sql Database with ado.net in Asp.Net Core Mvc 5.0? 我如何引用MVC项目中的类,该类使用asp.net核心从数据库中检索数据 - How can I reference a class in MVC project that retrieves data from database with asp.net core 我如何重定向一个ASP.NET MVC2页面 - How can I redirect an asp.net mvc2 page 如何在ASP.NET MVC2中向控制器添加自定义挂钩 - How to add custom hooks to controllers in ASP.NET MVC2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM