[英]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.