Im trying to configure EntityFramework with Npgsql with code first.
- EntityFramework 6.1.3
- Npgsql 3.0.5
Now, i want to set a custom configuration by a class, let's see:
public class DbConfig : DbConfiguration
{
public DbConfig()
{
SetProviderFactory("Npgsql", Npgsql.NpgsqlFactory.Instance);
SetProviderServices("Npgsql", provider: NpgsqlServices.Instance);
SetDefaultConnectionFactory(new NpgsqlConnectionFactory());
}
}
and my context class:
[DbConfigurationType(typeof(DbConfig))]
public class Db: DbContext
{
public virtual DbSet<Products> Products {get;set;}
public Db(DbConnection Connection): base(Connection, true)
{
}
public Db(): base()
{
}
public Db(string connectionString): base(connectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Add<CascadeDeleteAttributeConvention>();
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.HasDefaultSchema("public");
base.OnModelCreating(modelBuilder);
}
}
And I try to get access to Products:
class Program
{
static void Main(string[] args)
{
Db.ConfigureMigrations();
string connectionString = "Server=localhost;Port=5432;Database=stockDb;User Id=postgres;Password=123456;";
using (var C = new Db(connectionString))
{
var productos = C.Productos.Count();
Console.WriteLine(productos);
}
Console.WriteLine("listo");
Console.ReadKey();
}
}
But i have some exceptions:
- 'System.Data.Entity.Core.ProviderIncompatibleException' occurred in EntityFramework.dll. inner exception: {"The provider did not return a ProviderManifestToken string."}
or:
The format of the initialization string does not conform to specification starting at index 0.
And i don't know whats wrong.
I got reference:
https://msdn.microsoft.com/en-us/data/jj680699.aspx
csomeone can help me?
I solved this:
The problem is with Db.ConfigureMigrations(); line.
if i comment that line all right, but i need the migrations, then in my Db context in my ConfigureMigrations method i need to change for:
Database.SetInitializer(new MigrateDatabaseToLatestVersion(true));
And that'a all. Thanks.