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
);
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
}
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();
}
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();
}
}
}
Would you like to know where I'm going wrong?