Entity framework 移动服务(.Net后端)使用了不正确的连接字符串
我正在努力使移动服务的.Net后端使用正确的connectionString。发布服务时,我为“MS_TableConnectionString”选择了正确的连接字符串。如果我检查服务器上的web.config(通过FTP),我会看到我所期望的: 服务器上的web.config:Entity framework 移动服务(.Net后端)使用了不正确的连接字符串,entity-framework,azure,azure-mobile-services,Entity Framework,Azure,Azure Mobile Services,我正在努力使移动服务的.Net后端使用正确的connectionString。发布服务时,我为“MS_TableConnectionString”选择了正确的连接字符串。如果我检查服务器上的web.config(通过FTP),我会看到我所期望的: 服务器上的web.config: <connectionStrings> <add name="MS_TableConnectionString" connectionString="Data Source=tcp:[Server
<connectionStrings>
<add name="MS_TableConnectionString" connectionString="Data Source=tcp:[ServerAddress].database.windows.net,1433;Initial Catalog=[MyMobileService_db];Integrated Security=False;User ID=[correctUserName];Password=[CorrectPassword];Connect Timeout=30;Encrypt=True" providerName="System.Data.SqlClient" />
为了查看实际使用的连接字符串,我将其添加到示例控制器中:
客户机代码示例:
public class ExampleController : ApiController
{
MyMobileServiceContext context;
public ApiServices ApiServices { get; set; }
public ExampleController()
{
context = new MyMobileServiceContext();
}
public async Task<IHttpActionResult> PostExample(ExampleItem item)
{
ApiServices.Log.Warn("ConnectionString: " + context.Database.Connection.ConnectionString);
...
}
公共类示例控制器:ApiController
{
MyMobileServiceContext;
公共ApiServices ApiServices{get;set;}
公共示例控制器()
{
context=新的MyMobileServiceContext();
}
公共异步任务PostExample(ExampleItem)
{
ApiServices.Log.Warn(“ConnectionString:+context.Database.Connection.ConnectionString”);
...
}
当我查看移动服务上的日志条目时,我会看到不同的用户名和密码:
[2014-04-15T12:26:33.1410580Z]Level=Warn,Kind=Trace,Category='PostExampleItem',Id=00000000-0000-0000-0000-00000000,Message='ConnectionString:Data Source=[SameServerAddress].database.windows.net;初始目录=[SameDatabaseName];用户Id=[DifferentituserName];密码=[Differentitpassword];异步处理=True;TrustServerCertificate=False;'
不同的用户名和密码与我以SQLServerDBConnectionString的名称下载的原始.PublishSettings文件中看到的相同,但我不知道该文件存储在服务器上的何处
由于用户名和密码不同,我在日志中看到以下异常:
[2014-04-15T13:18:11.2007511Z]Level=Error,Kind=Trace,Category='App.Request',Id=d7ec6d25-f3b7-4e88-9024-217be40ae77f,Exception=System.Data.Entity.Core.ProviderIncompatibleException:访问数据库时出错。这通常意味着与数据库的连接失败。请检查连接字符串是否正确,以及相应的DbContext构造是否正确或用于指定它或在应用程序的配置文件中查找它。有关DbContext和连接的信息,请参阅。有关失败的详细信息,请参阅内部异常。-->System.Data.Entity.Core.ProviderIncompatibleException:提供程序未返回ProviderManifestToken字符串。-->System.Data.SqlClient.SqlException:无法操作登录请求的en数据库“master”。登录失败。
用户“[DifferentUserName]”登录失败。
已为此会话分配了跟踪ID“[GUID]”。请在需要帮助时向客户支持提供此跟踪ID
任何帮助都将不胜感激,因为目前我必须在上下文的构造函数中硬编码整个连接字符串以使其工作
谢谢
F
更新:2014年4月15日15:23
我删除了我的所有publisher配置文件,并创建了原始.PublishSettings文件的副本。从中,我删除了除一个配置文件以外的所有配置文件。然后,我删除了SQLDBConnectionString属性,以确认不是因为我发送了此配置文件才导致问题。结果没有变化,它仍在使用不同的用户名和密码因此,它一定是从服务器的某个地方读取的。我们现在有一个漏洞,我们从门户获取连接字符串,但没有在那里公开设置或修改连接字符串的功能 解决方法是在门户中设置应用程序设置,然后在代码中使用ApiServices类使用该设置,类似这样(在控制器中) 我知道这很混乱…我们将使访问和修改连接字符串更容易
亨里克你好,亨里克,谢谢你的快速回复和建议。我现在就实施。
public class ExampleController : ApiController
{
MyMobileServiceContext context;
public ApiServices ApiServices { get; set; }
public ExampleController()
{
context = new MyMobileServiceContext();
}
public async Task<IHttpActionResult> PostExample(ExampleItem item)
{
ApiServices.Log.Warn("ConnectionString: " + context.Database.Connection.ConnectionString);
...
}
string connectionString = this.Services.Settings["YourConnectionStringAsAppSetting"];