Wrong connection string passed during database update in IDesignTimeDbContextFactory implementation












0














I have a class library with the next structure:



namespace Layers
{
public class DatabaseLayer
{
private Database _database;

public string ConnectionString {get; set; }
}
}

namespace Models
{
public class DatabaseBase : DbContext, IDisposable
{
private DbConnection _connection;
private DbCommand _command;

public string ConnectionString {get; set; }
}

public class Database : DatabaseBase
{
internal Database(string connectionString) : base(connectionString) { }

public virtual DbSet<FileEntity> Files { get; set; }
}
}


I added an implementation of IDesignTimeDbContextFactory interface to avoid message Unable to create an object of type 'Database'. Add an implementation of 'IDesignTimeDbContextFactory' to the project:



namespace Models
{
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<Database>
{
public Database CreateDbContext(string args)
{
var builder = new DbContextOptionsBuilder<Database>();
var connectionString = @"Server=MyInstanceName;Database=MyDatabaseName;Trusted_Connection=True;";

builder.UseSqlServer(connectionString, b => b.MigrationsAssembly("MyAssemblyName"));

return new Database(builder.Options.ToString());
}
}
}


When I try to run update-database -Context Database, I get




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(Dictionary2
parsetable, String connectionString, Boolean buildChain, Dictionary
2
synonyms, Boolean firstKey) at
System.Data.Common.DbConnectionOptions..ctor(String connectionString,
Dictionary`2 synonyms)




It is obviously the problem is in my connectionString, but I copied it from testing project as-is and it works well there (connections test are passed).



What can I miss? I'm using EF Core and .NET Core 2.1.



Edit
I'm running my code under debugger and see there is wrong connection string sends to Database constructor:




"Microsoft.EntityFrameworkCore.DbContextOptions`1[DELay.Classes.DataModel.Database]"




The selected line contains right connection string and the next one - no. So my question should sound how to send connection string from DbContextOptionsBuilder to DbContext constructor correctly.



Edit2:
I can make it work by replacing



return new Database(builder.Options.ToString());


with



return new Database((builder.Options.Extensions.First() as 
SqlServerOptionsExtension).ConnectionString.ToString());


in CreateDbContext, but I'm not sure this is a proper way...










share|improve this question
























  • Without the ability to debug your code I have to take a long shot, but can you try to remove the "@" sign in the assignment of the ConnectionString variable? If you copied it form a working project, this is the only thing I can think of.
    – Jevgeni Geurtsen
    Nov 20 at 14:37












  • In real life there is a slash in the connection string: Server=HostNameInstanceName, so "@" is needed to avoid using "\" in string inside. But is doesn't work in both cases.
    – Miamy
    Nov 20 at 14:46












  • There are two Extentions appeared in builder.Options after UseSqlServer call, and the second is something what I don't need at all.
    – Miamy
    Nov 20 at 14:49
















0














I have a class library with the next structure:



namespace Layers
{
public class DatabaseLayer
{
private Database _database;

public string ConnectionString {get; set; }
}
}

namespace Models
{
public class DatabaseBase : DbContext, IDisposable
{
private DbConnection _connection;
private DbCommand _command;

public string ConnectionString {get; set; }
}

public class Database : DatabaseBase
{
internal Database(string connectionString) : base(connectionString) { }

public virtual DbSet<FileEntity> Files { get; set; }
}
}


I added an implementation of IDesignTimeDbContextFactory interface to avoid message Unable to create an object of type 'Database'. Add an implementation of 'IDesignTimeDbContextFactory' to the project:



namespace Models
{
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<Database>
{
public Database CreateDbContext(string args)
{
var builder = new DbContextOptionsBuilder<Database>();
var connectionString = @"Server=MyInstanceName;Database=MyDatabaseName;Trusted_Connection=True;";

builder.UseSqlServer(connectionString, b => b.MigrationsAssembly("MyAssemblyName"));

return new Database(builder.Options.ToString());
}
}
}


When I try to run update-database -Context Database, I get




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(Dictionary2
parsetable, String connectionString, Boolean buildChain, Dictionary
2
synonyms, Boolean firstKey) at
System.Data.Common.DbConnectionOptions..ctor(String connectionString,
Dictionary`2 synonyms)




It is obviously the problem is in my connectionString, but I copied it from testing project as-is and it works well there (connections test are passed).



What can I miss? I'm using EF Core and .NET Core 2.1.



Edit
I'm running my code under debugger and see there is wrong connection string sends to Database constructor:




"Microsoft.EntityFrameworkCore.DbContextOptions`1[DELay.Classes.DataModel.Database]"




The selected line contains right connection string and the next one - no. So my question should sound how to send connection string from DbContextOptionsBuilder to DbContext constructor correctly.



Edit2:
I can make it work by replacing



return new Database(builder.Options.ToString());


with



return new Database((builder.Options.Extensions.First() as 
SqlServerOptionsExtension).ConnectionString.ToString());


in CreateDbContext, but I'm not sure this is a proper way...










share|improve this question
























  • Without the ability to debug your code I have to take a long shot, but can you try to remove the "@" sign in the assignment of the ConnectionString variable? If you copied it form a working project, this is the only thing I can think of.
    – Jevgeni Geurtsen
    Nov 20 at 14:37












  • In real life there is a slash in the connection string: Server=HostNameInstanceName, so "@" is needed to avoid using "\" in string inside. But is doesn't work in both cases.
    – Miamy
    Nov 20 at 14:46












  • There are two Extentions appeared in builder.Options after UseSqlServer call, and the second is something what I don't need at all.
    – Miamy
    Nov 20 at 14:49














0












0








0







I have a class library with the next structure:



namespace Layers
{
public class DatabaseLayer
{
private Database _database;

public string ConnectionString {get; set; }
}
}

namespace Models
{
public class DatabaseBase : DbContext, IDisposable
{
private DbConnection _connection;
private DbCommand _command;

public string ConnectionString {get; set; }
}

public class Database : DatabaseBase
{
internal Database(string connectionString) : base(connectionString) { }

public virtual DbSet<FileEntity> Files { get; set; }
}
}


I added an implementation of IDesignTimeDbContextFactory interface to avoid message Unable to create an object of type 'Database'. Add an implementation of 'IDesignTimeDbContextFactory' to the project:



namespace Models
{
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<Database>
{
public Database CreateDbContext(string args)
{
var builder = new DbContextOptionsBuilder<Database>();
var connectionString = @"Server=MyInstanceName;Database=MyDatabaseName;Trusted_Connection=True;";

builder.UseSqlServer(connectionString, b => b.MigrationsAssembly("MyAssemblyName"));

return new Database(builder.Options.ToString());
}
}
}


When I try to run update-database -Context Database, I get




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(Dictionary2
parsetable, String connectionString, Boolean buildChain, Dictionary
2
synonyms, Boolean firstKey) at
System.Data.Common.DbConnectionOptions..ctor(String connectionString,
Dictionary`2 synonyms)




It is obviously the problem is in my connectionString, but I copied it from testing project as-is and it works well there (connections test are passed).



What can I miss? I'm using EF Core and .NET Core 2.1.



Edit
I'm running my code under debugger and see there is wrong connection string sends to Database constructor:




"Microsoft.EntityFrameworkCore.DbContextOptions`1[DELay.Classes.DataModel.Database]"




The selected line contains right connection string and the next one - no. So my question should sound how to send connection string from DbContextOptionsBuilder to DbContext constructor correctly.



Edit2:
I can make it work by replacing



return new Database(builder.Options.ToString());


with



return new Database((builder.Options.Extensions.First() as 
SqlServerOptionsExtension).ConnectionString.ToString());


in CreateDbContext, but I'm not sure this is a proper way...










share|improve this question















I have a class library with the next structure:



namespace Layers
{
public class DatabaseLayer
{
private Database _database;

public string ConnectionString {get; set; }
}
}

namespace Models
{
public class DatabaseBase : DbContext, IDisposable
{
private DbConnection _connection;
private DbCommand _command;

public string ConnectionString {get; set; }
}

public class Database : DatabaseBase
{
internal Database(string connectionString) : base(connectionString) { }

public virtual DbSet<FileEntity> Files { get; set; }
}
}


I added an implementation of IDesignTimeDbContextFactory interface to avoid message Unable to create an object of type 'Database'. Add an implementation of 'IDesignTimeDbContextFactory' to the project:



namespace Models
{
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<Database>
{
public Database CreateDbContext(string args)
{
var builder = new DbContextOptionsBuilder<Database>();
var connectionString = @"Server=MyInstanceName;Database=MyDatabaseName;Trusted_Connection=True;";

builder.UseSqlServer(connectionString, b => b.MigrationsAssembly("MyAssemblyName"));

return new Database(builder.Options.ToString());
}
}
}


When I try to run update-database -Context Database, I get




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(Dictionary2
parsetable, String connectionString, Boolean buildChain, Dictionary
2
synonyms, Boolean firstKey) at
System.Data.Common.DbConnectionOptions..ctor(String connectionString,
Dictionary`2 synonyms)




It is obviously the problem is in my connectionString, but I copied it from testing project as-is and it works well there (connections test are passed).



What can I miss? I'm using EF Core and .NET Core 2.1.



Edit
I'm running my code under debugger and see there is wrong connection string sends to Database constructor:




"Microsoft.EntityFrameworkCore.DbContextOptions`1[DELay.Classes.DataModel.Database]"




The selected line contains right connection string and the next one - no. So my question should sound how to send connection string from DbContextOptionsBuilder to DbContext constructor correctly.



Edit2:
I can make it work by replacing



return new Database(builder.Options.ToString());


with



return new Database((builder.Options.Extensions.First() as 
SqlServerOptionsExtension).ConnectionString.ToString());


in CreateDbContext, but I'm not sure this is a proper way...







c# entity-framework-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 14:55

























asked Nov 20 at 13:30









Miamy

1,042514




1,042514












  • Without the ability to debug your code I have to take a long shot, but can you try to remove the "@" sign in the assignment of the ConnectionString variable? If you copied it form a working project, this is the only thing I can think of.
    – Jevgeni Geurtsen
    Nov 20 at 14:37












  • In real life there is a slash in the connection string: Server=HostNameInstanceName, so "@" is needed to avoid using "\" in string inside. But is doesn't work in both cases.
    – Miamy
    Nov 20 at 14:46












  • There are two Extentions appeared in builder.Options after UseSqlServer call, and the second is something what I don't need at all.
    – Miamy
    Nov 20 at 14:49


















  • Without the ability to debug your code I have to take a long shot, but can you try to remove the "@" sign in the assignment of the ConnectionString variable? If you copied it form a working project, this is the only thing I can think of.
    – Jevgeni Geurtsen
    Nov 20 at 14:37












  • In real life there is a slash in the connection string: Server=HostNameInstanceName, so "@" is needed to avoid using "\" in string inside. But is doesn't work in both cases.
    – Miamy
    Nov 20 at 14:46












  • There are two Extentions appeared in builder.Options after UseSqlServer call, and the second is something what I don't need at all.
    – Miamy
    Nov 20 at 14:49
















Without the ability to debug your code I have to take a long shot, but can you try to remove the "@" sign in the assignment of the ConnectionString variable? If you copied it form a working project, this is the only thing I can think of.
– Jevgeni Geurtsen
Nov 20 at 14:37






Without the ability to debug your code I have to take a long shot, but can you try to remove the "@" sign in the assignment of the ConnectionString variable? If you copied it form a working project, this is the only thing I can think of.
– Jevgeni Geurtsen
Nov 20 at 14:37














In real life there is a slash in the connection string: Server=HostNameInstanceName, so "@" is needed to avoid using "\" in string inside. But is doesn't work in both cases.
– Miamy
Nov 20 at 14:46






In real life there is a slash in the connection string: Server=HostNameInstanceName, so "@" is needed to avoid using "\" in string inside. But is doesn't work in both cases.
– Miamy
Nov 20 at 14:46














There are two Extentions appeared in builder.Options after UseSqlServer call, and the second is something what I don't need at all.
– Miamy
Nov 20 at 14:49




There are two Extentions appeared in builder.Options after UseSqlServer call, and the second is something what I don't need at all.
– Miamy
Nov 20 at 14:49

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394117%2fwrong-connection-string-passed-during-database-update-in-idesigntimedbcontextfac%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394117%2fwrong-connection-string-passed-during-database-update-in-idesigntimedbcontextfac%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin