Alan Wayne
Alan Wayne

Reputation: 5384

How to build an Entity Framework model from a PostgreSQL schema?

(This question follows an excellent post on using Npgsql).

I have a PostgreSQL (9.5.0) database with two schemas: public and nova. I then used NuGet console:

Install-Package EntityFramework6.Npgsql -Version 3.0.5

Now I add the following element to the app.config file:

<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>

Then I added a new "ADO.NET Entity Data Model" to my project.

The following connection properties were filled in:

Database:       chaos
Host:           localhost
Password:       psw
Port:           5432
Search Path:    nova
Username:       postgres

The connection is then tested and everything works.

The Entity Framework is then built with database first. The .edmx file is built correctly and the VS designer is happy.

The problem is the .edmx file is built from all the tables in the database. That is, both the public and nova schema's tables are in the .edmx file.

In addition to specifying the search path in the connection properties, I have tried the following on the database--but these do not change both schemas being added to the .edmx file:

Is there any way to build the Entity Framework with only the one schema, Nova?

My components:

TIA

Upvotes: 2

Views: 8918

Answers (2)

Inshal Irshad
Inshal Irshad

Reputation: 451

As per documentation and quite bit research as I was working on similar problem, I found that with postgres using npgsql it is only possible to get model generation via Database First approach in .NET core framework as also mentioned by veuncent.

For Entity Framework 6 in .Net Framework there is no option exist as of today in npgsql.

Upvotes: 0

veuncent
veuncent

Reputation: 1712

You can use the Package Manager Console to run a Database First scaffolding.
Use the -Schemas parameter to only run it for specific schemas:

Scaffold-DbContext "host=server;database=chaos;user id=postgres;" Devart.Data.PostgreSql.Entity.EFCore -Schemas nova

The Scaffold-DbContext command also accepts a parameter for tables: -Tables table1,table2.

Answer taken from here.

See also this tutorial.

Upvotes: 2

Related Questions