首先在EF代码中调用update-database时连接字符串出错,但连接字符串是正确的(用其他工具检查过)

问题描述 投票:0回答:1

我有一个使用 EF 的测试应用程序,它正在使用 Context Factory 创建用于迁移的上下文。

我可以调用添加迁移,它会在我的项目中生成合适的迁移,但是当我调用更新数据库时,我收到错误。

上下文工厂如下

public class ContextFactory: IDesignTimeDbContextFactory<DBContext>
{
    public DBContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<DBContext>();
        
        optionsBuilder.UseSqlite("Data Source=C:\\tmp\\sqlite.db; Version=3;");
       
        return new DBContext(optionsBuilder.Options);
    }
}

我收到的错误如下:

PM> Update-Database
Build started...
Build succeeded.
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
   at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
   at System.Data.Common.DbConnectionOptions.ParseInternal(Dictionary`2 parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms, Boolean firstKey)
   at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms, Boolean useOdbcRules)
   at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
   at Microsoft.Data.Sqlite.SqliteConnectionStringBuilder..ctor(String connectionString)
   at Microsoft.Data.Sqlite.SqliteConnectionFactory.GetPoolGroup(String connectionString)
   at Microsoft.Data.Sqlite.SqliteConnection.set_ConnectionString(String value)
   at Microsoft.Data.Sqlite.SqliteConnection..ctor(String connectionString)
   at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.MigrateUsingConnection(IDiagnosticsLogger`1 diagnostics, IMigrator migrator, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Format of the initialization string does not conform to specification starting at index 0.

我检查了 SQLite 服务器紧凑工具箱并确认连接字符串有效。

问题是什么以及如何解决?我怎样才能获得更好的错误数据以便修复它?

c# sqlite entity-framework connection-string
1个回答
0
投票

我应用了你的代码,但出现了一些与你无关的错误

不支持连接字符串关键字

version

然后我从连接字符串中删除了关键字

Version=3;
,然后出现以下错误

无法打开数据库文件

然后我创建文件夹

c:\tmp
,最后它工作得很好

我认为你在某个地方更改了连接字符串,也许是在依赖注入中,或者通过覆盖

OnConfiguring

临时,将以下

OnConfiguring
方法添加到您的
DBContext
以确保问题确实来自连接字符串!

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 {
     optionsBuilder.UseSqlite("Data Source=C:\\tmp\\sqlite.db;");
 }
© www.soinside.com 2019 - 2024. All rights reserved.