Asp.Net mvc 5 connection postgresql



1. Open Your Visual Studio 2017
2. Tool Menu -> NuGet Package Manager -> Package Manager Console
Type Code: PM Console Area.
          PM> Install-Package Npgsql
PM> Install-Package Npgsql -Version 2.2.0
PM> Install-Package Npgsql.EntityFramework

Note Few Minutes After installing  then write your WebConfig code

Web.config

<configuration>
<connectionStrings>
<add name="DefaultConnectionString" connectionString="server=localhost;user id=postgres;password=admin;database=mydatabase" providerName="Npgsql" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql"/>
<add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" support="FF"/>
</DbProviderFactories>
</system.data>
--------
---------------------------------
</configuration>
----------------------------------------------------------------------
Model:
Product Class

public class Product
    {
        public int id { get; set; }
        public string name { get; set; }
        public string code { get; set; }
    }

PGDbContext Class
using System.Data.Entity;
public class PGDbContext:DbContext
    {
        public PGDbContext() : base(nameOrConnectionString: "DefaultConnectionString") { }
        public virtual DbSet<Product> Products { get; set; }
    }

---------------------------------------------------------------------------
Controller:
HomeController Class
using MyWebApp.Models;

PGDbContext _context;
        public HomeController()
        {
            _context = new PGDbContext();
        }
        public ActionResult Index()
        {
            var data = _context.Products.ToList();
            return View();
        }

Note: After Complete Run Your Application
Share on Google Plus

About Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment