[英]EntityFramework Npgsql DbConfiguration doesn't work
Im trying to configure EntityFramework with Npgsql with code first.我试图首先用代码配置 EntityFramework 和 Npgsql。
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:但我有一些例外:
or:或者:
The format of the initialization string does not conform to specification starting at index 0.初始化字符串的格式不符合从索引 0 开始的规范。
And i don't know whats wrong.我不知道怎么了。
I got reference:我得到了参考:
https://msdn.microsoft.com/en-us/data/jj680699.aspx https://msdn.microsoft.com/en-us/data/jj680699.aspx
csomeone can help me? csomeone 可以帮助我吗?
I solved this:我解决了这个问题:
The problem is with Db.ConfigureMigrations();问题在于 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:如果我对该行进行了注释,但我需要迁移,那么在我的 ConfigureMigrations 方法中的 Db 上下文中,我需要更改为:
Database.SetInitializer(new MigrateDatabaseToLatestVersion(true)); Database.SetInitializer(new MigrateDatabaseToLatestVersion(true));
And that'a all.这就是全部。 Thanks.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.