Basic example of postgres dotconnect with EF5?

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
betawarz
Posts: 10
Joined: Mon 06 Dec 2010 18:15

Basic example of postgres dotconnect with EF5?

Post by betawarz » Mon 14 Oct 2013 19:17

Hi all,

I'm writing an application, following the repository pattern, that uses Entity Framework 5. So, I have my own DbContext class already, but it currently just uses the default Sqlite local database. I'm now trying to figure out how to introduce dotconnect for postgres into this and make my DbContext connect to and use postgres.

Are there any examples of this anywhere? I've seen a few but they're all pretty complicated looking and the code is too messy for me to comprehend just the basics. Is there maybe a good explanation of what to do at the most simple level?

Thanks

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Basic example of postgres dotconnect with EF5?

Post by Shalex » Tue 15 Oct 2013 10:21

Here is a simple walkhrough:

1. Add a Devart Entity Model to your project, follow the wizard and select the Repository And Unit of Work and DbContext templates.
The "Repository ..." template generates an intermediate code which works like an interface between your code and the model but it doesn't generate the context itself. For this, we have added the "main" DbContext template to the model.

2. Select Repository And Unit of Work in Model Explorer > press F4 (navigate to Properties) > set its Use Db Context property to True. Save the model.

3. Open DataModel1.Designer.cs to find out the names of the used namespace and context (e.g.: PostgreModel and PostgreEntities). If you want to specify any Entity Framework Provider Configuration settings, it is better to do this in app.config (an example in the documentation) or in a static constructor: right click on the project in Solution Explorer > Add > New Item > Code > Class (Class1.cs) and put the following code into it:

Code: Select all

namespace PostgreModel {
    public partial class PostgreEntities {
        static PostgreEntities() {
            Devart.Data.PostgreSql.Entity.Configuration.PgSqlEntityProviderConfig config = Devart.Data.PostgreSql.Entity.Configuration.PgSqlEntityProviderConfig.Instance;
            config.DmlOptions.BatchUpdates.Enabled = true;
        }
    }
}
4. Save All, rebuild the application and retrieve data from the database using created repository. Does it work?

betawarz
Posts: 10
Joined: Mon 06 Dec 2010 18:15

Re: Basic example of postgres dotconnect with EF5?

Post by betawarz » Tue 15 Oct 2013 14:34

Thanks for the explanation. I'm actually doing this the code-first way, though. I'm manually writing my own POCO classes, my own DbContext and Repositories.

Right now, I just have the devart libraries referenced in my project, and in my connection string I say to use the devart provider. With just this, it seems to at least be able to query from my database. I don't know if there are other required steps, but I imagine there are because this seemed too easy.

Do you have a code-first explanation? Thanks.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Basic example of postgres dotconnect with EF5?

Post by Shalex » Wed 16 Oct 2013 08:08

The steps to start using dotConnect for PostgreSQL:

1.Replace your current connection string to the one which will use Devart.Data.PostgreSql. For example:

Code: Select all

<configuration>
  <connectionStrings>
    <add name="SAMPLEEntitiesConnectionString" connectionString="User ID=postgres;Password=postgres;Server=db;Database=postgres;"
      providerName="Devart.Data.PostgreSql" />
  </connectionStrings>
</configuration>
2. If you specified the types of the database columns via ColumnAttribute (the TypeName property) or, in case of fluent mapping, with the .HasColumnType(string columnType) method of property configuration in your application, you should change them to the PostgreSQL types.

Your code works via dotConnect for PostgreSQL smoothly, doesn't it? Have you encountered any difficulties?

Tips:
  • We recommend you to trace the SQL queries sent to the database via dbMonitor:
    http://www.devart.com/dotconnect/postgr ... nitor.html
    http://www.devart.com/dbmonitor/dbmon3.exe
  • Be aware about additional Entity Framework Provider Configuration options in dotConnect for PostgreSQL: http://www.devart.com/dotconnect/postgr ... ation.html. For example, you can configure the provider to recreate a database on each application run:

    Code: Select all

    // new Devart.Data.PostgreSql.PgSqlMonitor() { IsActive = true };
    var config = Devart.Data.PostgreSql.Entity.Configuration.PgSqlEntityProviderConfig.Instance;
    config.DatabaseScript.Schema.DeleteDatabaseBehaviour = Devart.Data.PostgreSql.Entity.Configuration.DeleteDatabaseBehaviour.Database;
    Database.SetInitializer<MyDbContext>(new DropCreateDatabaseAlways<MyDbContext>());
    new myDbContext().Database.Initialize(false);

Post Reply