简体   繁体   中英

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). 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. I want to connect an ASP.Net Core MVC app to a remote database using EF core.

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:

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:

{
  "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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM