Connection to ASP.NET CORE 2 database

Asked

Viewed 1,034 times

2

I’m starting with Asp.net, but specifically Asp.net core 2.

I’m reading the documentation contained in Docs.microsoft.com.

However, I have a question. How do I connect to a DB?

In PHP I use PDO, but in Asp.net core 2 I use what?

  • You’ll feel a big difference. As you are in the latest technology, I suggest you know the Nuget (package controller) and the Entity Framework (ORM) https://docs.microsoft.com/en-us/nuget/ https://docs.microsoft.com/en-us/efcore/

  • So, I’m studying the Entity Framework Core, but I still don’t really understand the connection itself. The use of it does. As far as I understand it is used for data modeling. Thanks for the links! I will look.

  • In Code First the tables are generated from the entities (the bank must already exist), but you can generate the entities at paritr of an existing bank. Remembering that the Entity Framework is native to SQL Server pro other banks have to download the specific version. The connection is inside webconfig and the connection name is passed as a parameter in Dbcontext. Check out my repository for a light inside the model folder https://github.com/krismorte/LigueMe

  • I understood, for example if I was to connect in SQL dB of Azure I would have to connect there. But if the table already exists in the database, dbset will run again?

  • Yeah, that’s right, too.

  • Krismorte, another question arose. What is ado.net? From what I saw it is a technology . Net for connection to dB. It replaces Entity? Man, sorry for the doubts but this world . Net is very large in view of Photoshop and with various embedded technologies.

  • ADO.NET is a set of predefined classes within the database platform for visual objects. Connection ado.net is usually fetia through only.

  • I’m beginning to understand. Today I read another part of the Entity documentation. I noticed that dbcontext makes the connection itself and it uses ado.net in the guts. To connect in a dB Azure I take the string ado net, put in appsettings and do dbcontext by Entity. I haven’t figured out how to save the data yet, I didn’t get to that.

Show 3 more comments

1 answer

1

  1. Install a version of SQL server communtity so you can do some tests.
  2. I created a new project in Asp.net core including individual authentication. By default Asp will create the connection string in the appsetting.json file similar to the line below:

"ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication3-AE3FA43F-A3AF-4A87-8999-727298B6B01F;Trusted_Connection=True;MultipleActiveResultSets=true" },

  1. Create your class templates in the Model folder of type: client, company and your data for example

    customer public class {

      [Key]
      public string nome { get; set; }
      public string cpf { get; set; }
    

    }

  2. Add a controller using the Display MVC Controller option, using the Entity Framework

  3. choose the class template you want to create views, controllers
  4. and the data model, you will probably find "Applicationdbcontext" as standard

In this class "Applicationdbcontext" you will find what you are looking for. Connection settings, you can configure some table properties, key Primary and etc..

In startup.Cs you can configure the type of database for Mysql or useSqlServer, I use Mysql. The default is sqlServer and you will find in this file

 services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

Remember the appsetting.json? file that’s where the connection string is called with the "Defaultconnection" databank. hope I’ve helped.

Browser other questions tagged

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