Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)


and also here is my app.config file:

XML
<?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>
Posted

1 solution

I have seen some issues before with |DataDirectory| and ClickOnce deployment. See the suggestion here: http://stackoverflow.com/questions/19252480/invalid-value-for-attachdbfilename[^] to replace the |DataDirectory| during deployment.

This is the code in VB.NET (from the link provided):
VB
Dim dataDir As String
If ApplicationDeployment.IsNetworkDeployed Then
    Dim ad = ApplicationDeployment.CurrentDeployment
    dataDir = ad.DataDirectory
Else
    dataDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
End If

The equivalent code in C# would be:
C#
string dataDir;
if (ApplicationDeployment.IsNetworkDeployed)
{
    var ad = ApplicationDeployment.CurrentDeployment;
    dataDir = ad.DataDirectory;
}
else
{
    dataDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
 
Share this answer
 
v2
Comments
Saeedek 3-Sep-14 9:08am    
Thank you but that was in vb i think.how can i apply that in c#?
kbrandwijk 3-Sep-14 9:30am    
I have updated the solution to include the C# translation, but I seriously doubt if you could not have translated one single if-statement from VB.NET to C# yourself...
Saeedek 3-Sep-14 9:36am    
so sorry about that thank you.and but where exactly put this code? in my config?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900