Mixing data between connections in dotConnect for PostgreSQL, version 7.10.1031

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
niton_dev
Posts: 20
Joined: Thu 22 Jul 2010 14:44

Mixing data between connections in dotConnect for PostgreSQL, version 7.10.1031

Post by niton_dev » Tue 28 Nov 2017 11:31

Problem description:
If I create a DbContext by passing a connection string to a non-existent database and trying to create a second DbContext with a connection string to another non-existent database, the second exception informs that it was not possible to connect the database informed in the first DbContext.

Test scenario:
Visual Studio 2017;
Windows 10;
Console Application .NET Core 2.0;

SQL Script:

Code: Select all

CREATE DATABASE foodb1;

CREATE TABLE foo (
    id integer NOT NULL primary key,
    name text
);
foo clas:

Code: Select all

public partial class foo {

        public foo()
        {
            OnCreated();
        }

        public virtual int id
        {
            get;
            set;
        }

        public virtual string name
        {
            get;
            set;
        }
    
        #region Extensibility Method Definitions

        partial void OnCreated();
        
        #endregion
    }
DbContext Code:

Code: Select all

public partial class FoodbModel : DbContext
    {
        private readonly string _host;
        private readonly int _port;
        private readonly string _database;
        private readonly string _user;
        private readonly string _passWord;

        public FoodbModel(string host, int port, string database, string user, string passWord) :
            base()
        {
            _host = host;
            _port = port;
            _database = database;
            _user = user;
            _passWord = passWord;
            OnCreated();
        }

        //public foodbModel(DbContextOptions<foodbModel> options) :
        //    base(options)
        //{
        //    OnCreated();
        //}

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionString = new PgSqlConnectionStringBuilder()
            {
                Host = _host,
                Database = _database,
                Port = _port,
                UserId = _user,
                Password = _passWord,
                LicenseKey = "PUT_YOUR_LICENCE_KEY_HERE"
            }.ConnectionString;

            if (!optionsBuilder.Options.Extensions.OfType<RelationalOptionsExtension>().Any(ext => !string.IsNullOrEmpty(ext.ConnectionString) || ext.Connection != null))
                optionsBuilder.UsePostgreSql(connectionString);
            CustomizeConfiguration(ref optionsBuilder);
            base.OnConfiguring(optionsBuilder);
        }

        partial void CustomizeConfiguration(ref DbContextOptionsBuilder optionsBuilder);

        public virtual DbSet<foo> foo
        {
            get;
            set;
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            this.fooMapping(modelBuilder);
            this.CustomizefooMapping(modelBuilder);

            RelationshipsMapping(modelBuilder);
            CustomizeMapping(ref modelBuilder);
        }
    
        #region foo Mapping

        private void fooMapping(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<foo>().ToTable(@"foo");
            modelBuilder.Entity<foo>().Property<int>(x => x.id).HasColumnName(@"id").HasColumnType(@"int4").IsRequired().ValueGeneratedOnAdd();
            modelBuilder.Entity<foo>().Property<string>(x => x.name).HasColumnName(@"name").HasColumnType(@"text").ValueGeneratedNever();
            modelBuilder.Entity<foo>().HasKey(@"id");
        }
	
        partial void CustomizefooMapping(ModelBuilder modelBuilder);

        #endregion

        private void RelationshipsMapping(ModelBuilder modelBuilder)
        {
        }

        partial void CustomizeMapping(ref ModelBuilder modelBuilder);

        public bool HasChanges()
        {
            return ChangeTracker.Entries().Any(e => e.State == Microsoft.EntityFrameworkCore.EntityState.Added || e.State == Microsoft.EntityFrameworkCore.EntityState.Modified || e.State == Microsoft.EntityFrameworkCore.EntityState.Deleted);
        }

        partial void OnCreated();
    }
Console program code:

Code: Select all

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //database foodb2 doesn't exists at localhost:5434
                using (var fooMd1 = new FoodbModel("localhost", 5434, "foodb2", "postgres", "put_password_here"))
                {
                    fooMd1.foo.Add(new foo() { id = 1, name = "Test 01" });
                    fooMd1.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }

            //Try connect in another database

            try
            {
                //database foodb3 doesn't exists at localhost:5434
                using (var fooMd2 = new FoodbModel("localhost", 5434, "foodb3", "postgres", "put_password_here"))
                {
                    fooMd2.foo.Add(new foo() { id = 2, name = "Test 02" });
                    fooMd2.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }
        }
    }
Program output:
Image

Would you like to know where I'm going wrong?

niton_dev
Posts: 20
Joined: Thu 22 Jul 2010 14:44

Re: Mixing data between connections in dotConnect for PostgreSQL, version 7.10.1031

Post by niton_dev » Thu 30 Nov 2017 19:43

Hello,
Could someone from devart help me?
Last edited by niton_dev on Fri 01 Dec 2017 13:46, edited 1 time in total.

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

Re: Mixing data between connections in dotConnect for PostgreSQL, version 7.10.1031

Post by Shalex » Fri 01 Dec 2017 10:58

Thank you for your report. We have reproduced the described behavior and are investigating it. We will notify you about the result.

niton_dev
Posts: 20
Joined: Thu 22 Jul 2010 14:44

Re: Mixing data between connections in dotConnect for PostgreSQL, version 7.10.1031

Post by niton_dev » Fri 01 Dec 2017 11:54

Thanks for the answer. Waiting for solution ...

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

Re: Mixing data between connections in dotConnect for PostgreSQL, version 7.10.1031

Post by Shalex » Thu 11 Jan 2018 20:42

The bug with opening connections, when talking to several non-existing databases, in EF Core is fixed: viewtopic.php?f=3&t=36447.

Post Reply