简体   繁体   中英

ASP.NET Core MVC with pgadmin show the table of database

I am new here, I use ADO.NET in ASP.NET MVC to connect model and database.

Now I want to use ASP.NET Core MVC to connect to Postgresql, but I don't know how to do.

I use "Connect Database" under "Tools", then I want show my table from my database, what should I do?

You should probably use the Entity Framework Core ORM. It has several database providers for PostgreSQL: Npgsql and devart – Peter Riesz

Right click on Project Solution select Manage Nuget Package and Install the following packages:

Npgsql.EntityFrameworkCore.PostgreSQL
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools

The version may vary at the time of install pay attention to the compatibility.

After the Installation, go to Startup.cs. In the ConfigureServices method we need to add PostgreSQL to the project Add your Database context (in my case MyWebApiContext) and the connection string name (ConnectionString is added in appsettings.json) (in my case MyWebApiConection).

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddEntityFrameworkNpgsql().AddDbContext<MyWebApiContext>(opt =>
            opt.UseNpgsql(Configuration.GetConnectionString("MyWebApiConection")));
    }

Configure Database Connection

In the appsetting.json, We need write PostgreSQL user id, password, server, port and the database name that will be created by code first.

    "ConnectionStrings": {
        "MyWebApiConection": "User ID =postgres;Password=1234;Server=localhost;Port=5432;Database=testDb; 
        Integrated Security=true;Pooling=true;"
    }

Connection string is added in the top of the appsettings.json, (Complete view of appsetting.json).

{
  "ConnectionStrings": {
    "MyWebApiConection": "User ID =postgres;Password=1234;Server=localhost;Port=5432;Database=testDb;Integrated Security=true;Pooling=true;"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

Create Models Create a folder and name it models (may be Entities), Add the Group, User and Context models.

   public class Group
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    // from the group model (Entity framework will connect the Primarykey and forign key)
    public Group Group { get; set; }
    public int GroupId { get; set; }
}

Now, Create a DbContext (In my case I named it as MyWebApiContext). This database context is inherited from the DbContext. DbContext error: check the entityFramework it may not be installed. MyWebApiContext > Database context name is add in the constructor.

public class MyWebApiContext:DbContext
    {
        public MyWebApiContext(DbContextOptions<MyWebApiContext> options):base(options) {  }
        public DbSet<User> Users { get; set; }
        public DbSet<Group> Groups { get; set; }
    }
}

Then perform migration

PM> enable-migrations
PM> add-migration initial
PM> update-database

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