我在共享服务器上运行ASP.NET MVC,我在通过System.Data.EntityClient连接到sql时遇到问题.下面是我的软件提供商给我连接到sql的连接字符串以及VS在开发期间为我的本地机器配置的连接字符串,当我部署到服务器时,我的连接字符串应该是什么样子?
来自我的托管服务商:
<add name="WeddingsDBEntities" connectionString="data Source=<server name>; Initial Catalog=<db name>; User ID=<user ID>; Password=<password>;" providerName="System.Data.EntityClient"/>
来自VS(开发期间):
connectionString="Metadata=res://*/Models.WeddingsModel.csdl|res://*/Models.WeddingsModel.ssdl|res://*/Models.WeddingsModel.msl;provider=System.Data.sqlClient;provider connection string="Data Source=.\sqlEXPRESS;AttachDbFilename=|DataDirectory|\WeddingsDB.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"
谢谢!
解决方法
您必须将连接字符串包装为实体连接字符串,格式为
<add name="Name" connectionString="Metadata=<Conceptual Model>|<Store Model>|<Mapping Model>; provider=<Underlying Connection Provider>; provider connection string="<Underlying ConnectionString>"" providerName="System.Data.EntityClient"/>
代替:
<add name="WeddingsDBEntities" connectionString="data Source=<server name>; Initial Catalog=<db name>; User ID=<user ID>; Password=<password>;" providerName="System.Data.EntityClient"/>
用这个:
<add name="WeddingsDBEntities" connectionString="Metadata=res://*/Models.WeddingsModel.csdl|res://*/Models.WeddingsModel.ssdl|res://*/Models.WeddingsModel.msl;provider=System.Data.sqlClient;provider connection string="data Source=<server name>; Initial Catalog=<db name>; User ID=<user ID>; Password=<password>;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>