Connect to database using . Net Core 2

Asked

Viewed 721 times

1

I am studying . Net Core 2 and I am unable to make the connection to the database.

My appsettings.json looks like this:

"ConnectionStrings": {
     "DefaultConnection": "Server=FAYOL\\SQLEXPRESS;Database=Pagamento;Trusted_Connection=True;MultipleActiveResultSets=true"
},

And the Startup.Cs

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Repository; 
namespace Radix
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

        var connection = @"Server=FAYOL\SQLEXPRESS;Database=Pagamento;Trusted_Connection=True;ConnectRetryCount=0";
        services.AddDbContext<Contexto>(options => options.UseSqlServer(connection));
    }
}

}

I’m not sure how to point out the Startup.cs for Json and how to do this. I took an example on the internet q puts the string directly in the ConfigureServices (as the example above) and is giving error.

Can you help me?

Thank you

  • Hello Thiago, in your Startup class in its constructor enter: public Startup(Iconfiguration Configuration) { Configuration = Configuration; } public Iconfiguration Configuration { get; } E then Configureservices: public void Configureservices(Iservicecollection services) { services.Adddbcontext<Dbcontextidentity>(options => options.Usesqlserver(Configuration.Getconnectionstring("Defaultconnection")); services.Addmvc(); ; }

1 answer

1


To recover the connection string from appsettings.json you can do it this way

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration
using Microsoft.Extensions.DependencyInjection;
using Repository; 
namespace Radix
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            string connecttionStringV2 = Configuration.GetSection("ConnectionStrings")["DefaultConnection"];
            string connectionString = Configuration.GetConnectionString("DefaultConnection");
            services.AddDbContext<Contexto>(options => options.UseSqlServer(connection));
        }
    }
}
  • Hi Barbetta, I tried it this way I tried it this way, but it says that Configuration.Getconnectionstring does not exist in context. If you use Microsoft.Extensions.Configuration there is no Getconnectionstring method and the same goes for System.Configuration.

  • Please add in question your entire startup, until the using's

  • Full class on the question. :)

  • @Thiagorodolfo updated, I think you accidentally deleted the builder.

  • Hi Barbetta, thanks for the elegance in showing my mistake, in fact I deleted without paying attention to what I was doing. : ) Another question, the line : string connectionString = Configuration.Getconnectionstring("Defaultconnection"); is returning null, the Configuration.Getconnectionstring("Defaultconnection"); does not find this string and it does not seem to be finding the appsettings.json.

  • Solved! Apparently my Program.Cs was wrong. Now it looks like this: public class Program { public Static void Main(string[] args) { Buildwebhost(args). Run(); } public Static Iwebhost Buildwebhost(string[] args) => Webhost.Createdefaultbuilder(args) . Usestartup<Startup>() . Build(); }

  • Aah yes, anyway I added a second way to call the connection string in the question

  • Give me another hand, please. When running the application goes to the url: http://localhost:52692/api/values and the following error: Could not find this localhost’s page. When I send it to the url of my http controller:/localhost:52692/api/Acquireecontroller/get, the error continues. I debugged Startup.Cs and Program.Cs and not from the error anywhere, I need to configure something else?

  • Try http://localhost:52692/api/Acquirer/Get, even though controllers have a controller in the name, it is not necessary to pass this on routes

  • Thanks Barbetta! Everything working perfectly now.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.