免责声明:即使之前在很多主题中都提到了这个错误,我也没有找到问题的答案;请在标记为重复之前随意回答,“因为它不是重复的:-)
我在ASP.NET MVC5数据库第一个项目中使用Identity Framework。
我最近在我的解决方案中做了很多改动,包括在AspNetUsers
表中添加一列。我已经完成了十几次,一切正常。
当我使用localhost中的所有新更改测试我的解决方案时,一切都运行良好,包括登录。
然后,我在我的PreProd环境中发布了我的代码和数据库更改,托管在Azure上(用于记录)
新的更改是可以的,但是,当我尝试登录到我的项目时,它失败了,我的日志中出现以下错误:
初始化字符串的格式不符合从索引0开始的规范。
问题出现在Login
HttpPost方法中,仅在调用UserManager
时
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
try
{
//Just to proove I can access to my DB
var businessLayer = new businessLayer();
var dataFromDB = businessLayer.GetDataFromMyDB();
logger.Info(dataFromDB ); //dataFromDB is the expected value from my DB
logger.Info("Will call UserManager");
var user = await UserManager.FindByEmailAsync(model.Email.Trim()); // <- Fails here in PreProd only
logger.Info(I never go here (in PreProd only)");
}
catch(Exception ex)
{
//Fails here in PreProd only with above error message
logger.Error(ex, ex.Message);
}
}
在localhost中一切都很好,在preprod(托管在Azure中)它在调用UserManager.FindByEmailAsync
时失败
这是我在localhost中的ConnectionString
<add name="MyEntities" connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MY-PC\SQLEXPRESS;initial catalog=MyDBName3;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
这是我的Azure Preprod的ConnectionString
<add name="MyEntities" connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;provider=System.Data.SqlClient;provider connection string="Server=tcp:xxxazure.database.windows.net,1433;Initial Catalog=MyDbName;Persist Security Info=False;User ID=MyUserID;Password=MyPassword;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;App=EntityFramework"" providerName="System.Data.EntityClient" />
当我在发布模式下使用VisualStudio的PreProd连接字符串时,完全没有问题我可以登录(?!?!)所以看起来问题来自PreProd环境本身,不是吗?
到目前为止我尝试过的:
"
改变'
Integrated Security
和其他bool值谁能告诉我:
非常感谢
只需要试一试:
如果您的代码中有如下所示的连接字符串
<add name="Entities" connectionString="metadata=res://*/TestDB.csdl|res://*/TestDB.ssdl|res://*/TestDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=XXXXXXXX.database.windows.net,1433;Initial Catalog=YourDB;User ID=YourUser;Password=XXXXXX"" providerName="System.Data.EntityClient" />
在应用程序设置中的azure portal中添加:
Name Column => Entities
Value Column => metadata=res://*/TestDB.csdl|res://*/TestDB.ssdl|res://*/TestDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=XXXXXXXX.database.windows.net,1433;Initial Catalog=YourDB;User ID=YourUser;Password=XXXXXX"
"Custom" - In the drop selection box
请确保在azure门户中更改“to”。
要从“普通”连接字符串转换为EF接受的字符串:
1)应用程序设置中的连接字符串类型必须是“其他”而不是“SQL Azure”
2)连接字符串值自动替换web.config中发布的任何内容
尝试这些选项,看看它是否有效。