Yod*_*oda 6 database sql-server asp.net-mvc connection-string web-config
在本地机器上开发ASP.NET-MVC应用程序期间,我使用它connectionString没有任何问题:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\AppDb.mdf;Initial Catalog=AppDb;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
在托管服务数据库控制面板中,我可以获得连接到我的MSSQL的连接字符串(我选择了SQL Server 2012数据库,但如果有帮助我可以选择2014).他们说连接字符串是:
"Data Source=SQL5013.myASP.NET;Initial Catalog=DB_9B42A0_baza;User Id=DB_9B42A0_baza_admin;Password=YOUR_DB_PASSWORD;"
Run Code Online (Sandbox Code Playgroud)
有关我的MSSQL数据库的信息:
Server name : SQL5013
Server version : Microsoft SQL Server 2012 - 11.0.5058.0 (X64)
May 14 2014 18:34:29
Standard Edition (64-bit) on Windows NT 6.3 (Build 9600: )
Database name:DB_9B42A0_baza
Server URL:SQL5013.myASP.NET
Login name:DB_9B42A0_baza_admin
Run Code Online (Sandbox Code Playgroud)
我的应用程序是带有Entity Framework 6的ASP.NET-MVC5.1.
这是我尝试过的:
添加了此连接定义 <connectionStrings> </connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=SQL5013.myASP.NET;Initial Catalog=DB_9B42A0_baza;User Id=DB_9B42A0_baza_admin;Password=12345678;" providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)
访问我的网站时的结果:
Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
Run Code Online (Sandbox Code Playgroud)
<add name="DefaultConnection" connectionString="Provider=sqloledb;Data Source=SQL5013,1433;Initial Catalog=DB_9B42A0_baza;User Id=DB_9B42A0_baza_admin;Password=12345678;" providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)
访问我的网站时的结果:
Exception Details: System.ArgumentException: Keyword not supported: 'provider'.
Run Code Online (Sandbox Code Playgroud)
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\AppDb.mdf;Initial Catalog=AppDb;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)
另外在WebApplication2中(这是我的ASP.NET-MVC应用程序的名称)属性:

访问我的网站时的结果:
Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
Run Code Online (Sandbox Code Playgroud)
<add name="DefaultConnection" connectionString="Server=SQL5013.myASP.NET;Database=DB_9B42A0_baza;User Id=DB_9B42A0_baza_admin;Password=12345678;" providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)
访问我的网站时的结果:
Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
Run Code Online (Sandbox Code Playgroud)
在过去的3天里,我尝试了很多让它工作的东西,我不记得了.我阅读了很多MSDN文章但没有运气.如果我可以提供有关数据库或应用程序的更多信息,请告诉我稍后将更新帖子.
<connectionStrings> </connectionStrings>在发布数据库连接之后,我应该在seciton中写什么 来使数据库连接正常工作?
我的完整Web.config档案是:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\AppDb.mdf;Initial Catalog=AppDb;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<customErrors mode="Off"/>
</system.web>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthenticationModule" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)
Application_Start 在 Global.asax
namespace WebApplication2 {
public class MvcApplication : System.Web.HttpApplication {
protected void Application_Start() {
System.Diagnostics.Debug.WriteLine("Application_Start");
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
new ApplicationDbContext().Database.Initialize(true);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和Configuration/Migrations.cs文件:
namespace WebApplication2.Migrations {
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Data.Entity.Validation;
using System.Linq;
using WebApplication2.Models;
internal sealed class Configuration : DbMigrationsConfiguration<WebApplication2.Models.ApplicationDbContext> {
public Configuration() {
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = "WebApplication2.Models.ApplicationDbContext";
}
protected override void Seed(WebApplication2.Models.ApplicationDbContext context) {
System.Diagnostics.Debug.WriteLine("SEED STARTED");
}
}
}
Run Code Online (Sandbox Code Playgroud)
而Models/IdentityModels.cs其中我的DbContext定义:
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System;
using System.Collections.Generic;
namespace WebApplication2.Models {
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser {
USER PROPERTIES HERE
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false) {
System.Diagnostics.Debug.WriteLine("CONSTRUCTOR");
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
}
DBSETS HERE
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Person>().HasMany(p => p.Answers).WithMany(a => a.Persons);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public static ApplicationDbContext Create() {
return new ApplicationDbContext();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没有资格在提出问题2天之前开始赏金,但是如果这有助于我为工作连接字符串提供500点声誉点(我会在赏金时奖励它).这对我来说太难了,我在3天内尝试了无数的东西.
Ton*_*ark 12
错误说明,尝试2中的连接字符串不正确."尝试3"中的连接字符串指向本地.但是,尝试1和4看起来完全有效.
您是否尝试使用right click on the project-->Publish而不是去发布网站project properties-->Package/Publish SQL?
请注意,使用时Project properties-->Package/Publish SQL,它不会更新目标上的web.config,并且需要Web配置转换.如果您尚未使用转换,则连接字符串将指向您在本地的字符串.
使用right click on the project-->Publish您可以提供目标连接字符串,对其进行测试,然后甚至可以web.config在部署期间进行此更新.确保Use this connection string at runtime (update destination web.config)已选中.

更新:
关于你的问题 - " 为什么我以前的尝试失败了? "
如上所述,尝试1和4看起来像你有一个有效的连接字符串但仍然得到错误
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
看来这种错误可能由于各种原因而发生 - 因为这里和这里提出的类似问题有不同的解决方案.但最有可能的是连接字符串不正确.
唯一可以确保的方法是,在部署/发布到托管服务后检查web.config中的内容.如果您确定部署后的web.config与尝试1和4具有相同的连接字符串,那么确实很奇怪.
此外,在尝试3中,Connection string for destination database看起来与您的其他连接字符串不同并且是否经过测试?这和我上面提到的Web发布方法的工作而不是使用这一事实Package/Publish SQL使我认为数据库可能尚未在您之前的尝试中部署.为什么不重复相同的步骤,但将其部署到您访问web.config的位置,还要检查数据库是否已成功部署?
现在, - " ?怎么会部分或Web.config中应完全像在我的情况下,使服务器上发布的Web应用程序后,数据库的工作,无所添加到发布的Web设置选项卡 "
通过不在工具Database中的Settings选项卡部分中设置任何内容Publish Web- 将不会部署您的数据库.考虑到您正在使用迁移,我不确定您是否愿意这样做.
但是,如果您计划出于任何原因单独部署数据库 - 那么您需要将转换应用于web.config,以使其在发布时自动更改连接字符串.
有关如何进行web.config转换的更多信息,请参见此处.
以下页面是理解Web和数据库发布的好地方
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
11526 次 |
| 最近记录: |