问题描述
我有一个在.Net 4下的程序,它使用实体框架模型将数据库用于数据。在Visual Studio中,我运行并调试程序没有任何问题。但是在我用oneclick发布(从构建菜单)发布我的程序并将其传输到运行安装了.Net framework 4的Windows XP的目标机器之后。程序安装得很好但是当我尝试一个想要读取的函数时或访问数据。它给了我这个错误:
[]
这里是完整的错误详情:
I have a program which is under .Net 4 which uses a database for data by using entity framework model. In the Visual Studio I run and debug the program without any problem. But after I publish my program with oneclick publish (from the build menu) and transfer it to a target machine which is running Windows XP with the .Net framework 4 installed.the program installs just fine but when I try a function which wants to read or access the data. It gives me this error:
http://i.stack.imgur.com/dZOe9.jpg[^]
and here is the full error details:
************** Exception Text **************
System.Data.EntityException: The underlying provider failed on ConnectionString. ---> System.ArgumentException: Invalid value for key 'attachdbfilename'.
at System.Data.SqlClient.SqlConnectionString.VerifyLocalHostAndFixup(String& host, Boolean enforceLocalHost, Boolean fixup)
at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
at System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value)
at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
--- End of inner exception stack trace ---
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)
at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)
at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at PachinFood.ViewCustomerForm.ViewCustomerForm_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
以及这里是我的app.config文件:
and also here is my app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<connectionStrings>
<add name="PachinDbEntities" connectionString="metadata=res://*/PachinModel.csdl|res://*/PachinModel.ssdl|res://*/PachinModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\PachinDb.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
推荐答案
Dim dataDir As String
If ApplicationDeployment.IsNetworkDeployed Then
Dim ad = ApplicationDeployment.CurrentDeployment
dataDir = ad.DataDirectory
Else
dataDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
End If
C#中的等效代码为:
The equivalent code in C# would be:
string dataDir;
if (ApplicationDeployment.IsNetworkDeployed)
{
var ad = ApplicationDeployment.CurrentDeployment;
dataDir = ad.DataDirectory;
}
else
{
dataDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
这篇关于部署winform应用程序错误:基础提供程序在ConnectionString上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!