dotConnect for PostgreSQL Documentation
In This Topic
    Entity Framework Support
    In This Topic

    The latest ADO.NET evolution focuses on raising the level of abstraction from the logical (relational) level to the conceptual (entity) level. For this purpose Microsoft introduces the Entity Framework, designed to simplify data - object conversion and embed data access queries into program code.

    dotConnect for PostgreSQL fully supports ADO.NET Entity Framework, including Entity Framework Core.

    The article consists of the following sections:

    What is ADO.NET Entity Framework?

    The Entity Framework is a set of technologies in ADO.NET that support development of data-oriented software applications. The Entity Framework is designed to enable developers to create data access applications by programming against a conceptual application model instead of programming directly against a relational storage schema. The goal is to decrease the amount of code and maintenance required for data-oriented applications. Entity Framework applications provide the following benefits:

    Entity Framework Support Key Features

    Using the Entity Data Provider

    dotConnect for PostgreSQL allows using it in Entity Framework models in various ways. You can use our provider with standard Visual Studio Entity Framework tools, in the same way as SqlClient.

    For example, you can create an Entity Framework model, using database-first approach (for Entity Framework v1 - v6) in the following way:

    1. Open your project.
    2. Add new item ADO.NET Entity Data Model to your project and specify its name.
    3. Choose Generate from database.
    4. Choose the dotConnect you use as data provider and specify the connection string.
    5. Select tables that are actually used in your project and click Generate to finish the process. If the selected tables contain foreign keys, they will be reflected in the generated models as well.
    6. Specify a name for the model namespace. Note that this name should be different from the database (schema) name.

    dotConnect for PostgreSQL also provides advanced ORM designer and code generation tool - Entity Developer. It allows creating Entity Framework v1 and Entity Framework Core models using database-first, model-first, or mixed approaches and generating code for them.

    For your convenience there is a brief Entity Framework Tutorial that you can use to get acquainted with the technology. You can also take a look at Entity Framework samples available with the product, or download separate Entity Framework Query Samples package. The latter is a standard Microsoft demo with added PostgreSQL connectivity.

    dotConnect for PostgreSQL also supports Visual Studio LightSwitch, so you can develop LightSwitch applications with our provider. You can find a tutorial on creating a simple LightSwitch application using dotConnect for PostgreSQL here: Tutorial: How to Connect Visual Studio LightSwitch to PostgreSQL with dotConnect for PostgreSQL Data Provider.

    Using the Entity Data Provider with Entity Framework v6

    Since there are no global Entity Framework provider registration for Entity Framework v6, you should register our provider manually in the config file of your project. For this you should add the following line:

    <provider invariantName="Devart.Data.PostgreSql" type="Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices, 
    Devart.Data.PostgreSql.Entity.EF6, Version=7.1.26.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
    

    to the entityFramework -> providers section.

    <entityFramework>
        <providers>
          <provider invariantName="Devart.Data.PostgreSql" type="Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices, 
          Devart.Data.PostgreSql.Entity.EF6, Version=7.1.26.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
        </providers>
    </entityFramework>
    

    Note: replace 7.1.26.0 with the actual assembly version.

    If it suits for your project, you may omit the exact assembly version:

    <entityFramework>
        <providers>
            <provider invariantName="Devart.Data.PostgreSql" type="Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices, Devart.Data.PostgreSql.Entity.EF6" />
        </providers>
    </entityFramework>
    

    This section appears in the project config file only after you add the NuGet EntityFramework package either explicitly, by executing the "install-package EntityFramework" command, or implicitly, by adding an Entity Framework v6 model for SQL Server via EDM Wizard.

    Using the Entity Data Provider with Entity Framework 6.4

    dotConnect for PostgreSQL fully supports Entity Framework 6.4 on .NET Core 3 (and on Full .NET Framework).

    For Entity Framework 6.4, an Entity Framework provider cannot be registered in the config file of your project. You need to use any of the following two code-based approaches:

    Both of the above examples use the PgSqlEntityProviderServicesConfiguration class that suites for most users. If Entity Framework 6.4 provider configuration must be customized, you will need to create a descendant of the DbConfiguration class and set values for SetProviderFactory() and SetProviderServices() in it. Then use this custom class for provider registration. Here is an example:

      public class PgSqlConfiguration: DbConfiguration {
     
        public PgSqlConfiguration() {
     
          SetProviderFactory("Devart.Data.PostgreSql", Devart.Data.PostgreSql.PgSqlProviderFactory.Instance);
          SetProviderServices("Devart.Data.PostgreSql", Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices.Instance);
        } 
      }

    Using the Entity Data Provider with Entity Framework Core

    dotConnect for PostgreSQL currently supports Entity Framework Core. Entity Framework Core is supported for the Full .NET Framework platform of version 4.5.1 and higher and for .NET Core. Dynamic database object creation (tables/FK/PK/indexes/triggers/sequences) based on an Entity Framework model is supported. See our Entity Framework Core Code-First Tutorial for Full .NET Framework and for .NET Core.

    Database-First via Package Manager Console and the Scaffold-DbContext command is supported. See our Entity Framework Core Database-First Tutorial for Full .NET Framework and for .NET Core.

    Please note that only a part of Entity Framework provider configuration settings is supported for Entity Framework Core. Also, not all Entity Framework v6 features are supported because Entity Framework Core does not support a lot of Entity Framework v6 features itself.

    In order to use Entity Framework Core for Full .NET Framework, you need Visual Studio 2013 or higher. For .NET Core you need Visual Studio 2017 or higher.

    In order to register an Entity Framework provider and set a connection string, you need to override the OnConfiguring method of your DbContext class descendant.

    C#csharpCopy Code
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
      
      optionsBuilder.UsePostgreSql(@"host=server;database=test;user id=postgres;");
    }
    
    
    Protected Overrides Sub OnConfiguring(optionsBuilder As DbContextOptionsBuilder)
    
    	optionsBuilder.UsePostgreSql("host=server;database=test;user id=postgres;")
    End Sub
    
    

    The mapping supported by Entity Framework Core is described in details in the corresponding section of Entity Framework documentation. If you plan to support several data sources for one base Entity Framework model, that is mapped to tables and columns named differently in different data sources, you can use special provider-specific extension methods in fluent mapping in order to support specific mapping for each data source simultaneously. dotConnect for PostgreSQL provides the following extension methods:

    • ForPostgreSqlToTable() - specifies the table name.
    • ForPostgreSqlHasColumnName() - specifies the column name.
    • ForPostgreSqlHasColumnType() - specifies the column type.
    • ForPostgreSqlHasName() - specifies the the name of the index and primary key.
    • ForPostgreSqlHasConstraintName() - specifies the foreign key.
    • ForPostgreSqlHasDefaultValueSql() - specifies the column default value.

    Deploying an Entity Framework Project

    When deploying Entity Framework projects, it is necessary to:

    1. Check if the required assemblies Devat.Data.dll, Devart.Data.PostgreSql.dll, and Devart.Data.PostgreSql.Entity.EF<Version>.dll are available for your application. The <Version> here is the version of Entity Framework used in your application. For Entity Framework v1 the assembly is called Devart.Data.PostgreSql.Entity.EF1.dll "1", for Entity Framework v4 it is Devart.Data.PostgreSql.Entity.EF4.dll, for Entity Framework v5 and v6 it is Devart.Data.PostgreSql.Entity.EF5.dll or Devart.Data.PostgreSql.Entity.EF6.dll respectively. For Entity Framework Core it is Devart.Data.PostgreSql.Entity.EFCore.dll. The assembly should reside in the application's folder or in GAC.

    2. If you use Code-First Migrations feature of Entity Framework v4 or Entity Framework v5, you also need to check if the Devart.Data.PostgreSql.Entity.Migrations.EF<Version>.dll assembly is available for your application. For Entity Framework v6 this assembly is not needed.
    3. If you use Entity Framework Spatials and NetTopologySuite, you also need to check if the Devart.Data.PostgreSql.Entity.Spatials.EF<Version>.dll assembly is available for your application.
    4. If you use Entity Framework Core Spatials and NetTopologySuite for Entity Framework Core 3, you need to check if the Devart.Data.PostgreSql.Entity.EFCore3.NetTopologySuite.dll assembly is available for your application. It is located in the \Entity\EFCore3 subfolders of the dotConnect for PostgreSQL installation folder. Additionally, you need to deploy the NetTopologySuite.dll assempbly from the NetTopologySuite 2.1.0 nuget package.
    5. For Entity Framework v1 - v6, you need to inform your environment about the provider factory existence (add the entry to the DbProviderFactories section as it is described above).
    <system.data>
      <DbProviderFactories>
        <remove invariant="Devart.Data.PostgreSql" />
        <add name="dotConnect for PostgreSQL" invariant="Devart.Data.PostgreSql"
         description="Devart dotConnect for PostgreSQL"
         type="Devart.Data.PostgreSql.PgSqlProviderFactory, Devart.Data.PostgreSql,
           Version=7.1.26.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
      </DbProviderFactories>
    </system.data>
    

    Replace 7.1.26.0 here with your actual version.

    You can find the necessary assemblies in the Entity folder in the provider installation folder, in the subfolder, corresponding to the Entity Framework version necessary.

    If you are registering the provider in the machine.config file, please take into account the Entity Framework version. Entity Framework v1 - specific applications require an entry in the .NET Framework 2.0 configuration file, and Entity Framework v4 and later - specific applications should be registered in the .NET Framework 4.0 configuration file. For Entity Framework Core there is no need to register the provider in the machine.config file.

    You can also read Micrsoft's Deployment Considerations on ADO.NET Entity Framework.

    Compatibility and Requirements

    dotConnect for PostgreSQL is compatible with the latest version of ADO.NET Entity Framework and Visual Studio. The following PostgreSQL version is required for Entity Framework to function properly: 8.0 or higher.

    Migration between ADO.NET Entity Data Model (*.edmx) and Devart Entity Model (*.edml)

    To migrate your ADO.NET Entity Data Model to Devart Entity Model, change the extension of the model file to edml (edmx -> edml), set the Build Action property of your model to "DevartEntityDeploy", and Custom Tool to "DevartEfGenerator". After that the model can be edited with the Devart Entity Developer.

    To migrate your Devart Entity Model to ADO.NET Entity Data Model, change the model file extension (edml -> edmx), set the Build Action property of your model to "EntityDeploy", and Custom Tool to "EntityModelCodeGenerator". After that the model can be edited with Entity Designer.

    Both *.edml and *.edmx can be opened and edited manually using XML Editor.

    See Also

    Entity Framework Tutorial | Devart LinqConnect | http://blog.devart.com/tag/entity-framework