1

I've created a WPF application in VS17. I used Entity Framework code first Version 6.0 for data. I also used some third-party libraries such as Newtonsoft.Json and EPPlus. I installed all of these through NuGet command line.

When I run the application in (with and without debugging) it works perfectly fine just as intended. I Published the application using the simple ClickOnce approach. The application publishes and I'm able to set it up. The application starts fine. Once I click a button to go to a section of the application, the application crashes.

I viewed the Windows Event viewer and know that it has something to do with deploying a database with Entity Framework. Here is the event log

Application: FBAContentApp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentException
   at System.Data.Entity.Core.Common.DbProviderServices.ExpandDataDirectory(System.String)
   at System.Data.Entity.SqlServer.SqlProviderServices.GetOrGenerateDatabaseNameAndGetFileNames(System.Data.SqlClient.SqlConnection, System.String ByRef, System.String ByRef, System.String ByRef)
   at System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(System.Data.Common.DbConnection, System.Nullable`1<Int32>, System.Data.Entity.Core.Metadata.Edm.StoreItemCollection)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(System.Data.Common.DbConnection, System.Nullable`1<Int32>, System.Data.Entity.Core.Metadata.Edm.StoreItemCollection)
   at System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
   at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(System.Data.Common.DbConnection)
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(System.Action)
   at System.Data.Entity.Migrations.DbMigrator.Update(System.String)
   at System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(System.Data.Entity.Internal.InternalContext, System.Func`3<System.Data.Entity.Migrations.DbMigrationsConfiguration,System.Data.Entity.DbContext,System.Data.Entity.Migrations.Infrastructure.MigratorBase>, System.Data.Entity.Core.Objects.ObjectContext)
   at System.Data.Entity.Internal.InternalContext.CreateDatabase(System.Data.Entity.Core.Objects.ObjectContext, System.Data.Entity.Internal.DatabaseExistenceState)
   at System.Data.Entity.Database.Create(System.Data.Entity.Internal.DatabaseExistenceState)
   at System.Data.Entity.CreateDatabaseIfNotExists`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].InitializeDatabase(System.__Canon)
   at System.Data.Entity.Internal.InternalContext+<>c__DisplayClassf`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].<CreateInitializationAction>b__e()
   at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(System.Action)
   at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
   at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(System.Data.Entity.Internal.InternalContext)
   at System.Data.Entity.Internal.RetryAction`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].PerformAction(System.__Canon)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(System.Action`1<System.Data.Entity.Internal.InternalContext>)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(System.Type)
   at System.Data.Entity.Internal.Linq.InternalSet`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Include(System.String)
   at System.Data.Entity.Infrastructure.DbQuery`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Include(System.String)
   at System.Data.Entity.QueryableExtensions.Include[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Linq.IQueryable`1<System.__Canon>, System.String)
   at System.Data.Entity.QueryableExtensions.Include[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Linq.IQueryable`1<System.__Canon>, System.Linq.Expressions.Expression`1<System.Func`2<System.__Canon,System.__Canon>>)
   at FBAContentApp.ViewModels.ProcessShipmentViewModel.PopulateAmazonWarehouse()
   at FBAContentApp.ViewModels.ProcessShipmentViewModel..ctor()
   at FBAContentApp.Views.NewShipment.PopulateGUI()
   at FBAContentApp.Views.NewShipment..ctor()
   at FBAContentApp.Views.MainMenu.NewShipment_Button_Click(System.Object, System.Windows.RoutedEventArgs)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
   at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
   at System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs)
   at System.Windows.UIElement.OnMouseLeftButtonUpThunk(System.Object, System.Windows.Input.MouseButtonEventArgs)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate, System.Object)
   at System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate, System.Object)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
   at System.Windows.UIElement.ReRaiseEventAs(System.Windows.DependencyObject, System.Windows.RoutedEventArgs, System.Windows.RoutedEvent)
   at System.Windows.UIElement.OnMouseUpThunk(System.Object, System.Windows.Input.MouseButtonEventArgs)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate, System.Object)
   at System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate, System.Object)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
   at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
   at System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs)
   at System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs, Boolean)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs)
   at System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr, System.Windows.Input.InputMode, Int32, System.Windows.Input.RawMouseActions, Int32, Int32, Int32)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr, MS.Internal.Interop.WindowMessage, IntPtr, IntPtr, Boolean ByRef)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Application.RunDispatcher(System.Object)
   at System.Windows.Application.RunInternal(System.Windows.Window)
   at System.Windows.Application.Run(System.Windows.Window)
   at System.Windows.Application.Run()
   at FBAContentApp.App.Main()

Furthermore, this 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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="FBAContentApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <userSettings>
    <FBAContentApp.Properties.Settings>
      <setting name="CompanyAddressId" serializeAs="String">
        <value>1</value>
      </setting>
      <setting name="LabelPrinter" serializeAs="String">
        <value />
      </setting>
      <setting name="SaveFileDir" serializeAs="String">
        <value />
      </setting>
    </FBAContentApp.Properties.Settings>
  </userSettings>
</configuration>

Here is my DbContext class

class AppContext : DbContext
{
    public DbSet<AmazonWarehouse> AmazonWarehouses { get; set; }

    public DbSet<CompanyAddress> CompanyAddresses { get; set; }

    public DbSet<Shipment> Shipments { get; set; }

    public DbSet<ShipmentBox> Boxes { get; set; }

    public DbSet<State> States { get; set; }

}

I've looked at this post that sounds similar, but my application is actually starting and then crashing when moving to a section where the database is queried.

I should also mention that I used Migrations (not sure if that matters much). In the Configuration.cs, the only method I've overridden is the Seed() method to populate beginning data.

Will someone please point me in the right direction as to how to fix this?

If you need additional info please let me. I appreciate anyone's time who's willing to help.

EDIT/UPDATE 1 Adding the connection string didn't work, as suggested by Dipen Shah. It caused the application to crash in debug mode. Per sTrenat, here's the code for ProcessShipmentViewModel.PopulateAmazonWarehouse()

/// <summary>
/// Grabs all AmazonWarehouses from the DbContext to populate the ListBox for the NewShipment.xaml view.
/// </summary>
private void PopulateAmazonWarehouse()
{
    using (var db = new Models.AppContext())
    {
        List<AmazonWarehouse> warehousees = db.AmazonWarehouses.Include(s => s.State).ToList();

        foreach (AmazonWarehouse amz in warehousees)
        {
            AmzWarehouseModel amzModel = new AmzWarehouseModel(amz);
            AmzWarehouses.Add(amzModel); 
           //AmzWarehouses is a List<AmazonWarehouseModel>. Which is a separate model from the Entity model (AmazonWarehouse)

        }

    }
}
3
  • Where is your connection string? I think that might be issue. Commented Aug 21, 2018 at 23:46
  • showing us ProcessShipmentViewModel.PopulateAmazonWarehouse() method should help us to resolve issue Commented Aug 22, 2018 at 6:20
  • some reasearches on google says you could have issue with your connection string with DataDirectory param. If so you could use AppDomain.CurrentDomain.GetData("DataDirectory") to get your correct Directory and then subs your old from connectionString Commented Aug 22, 2018 at 6:33

3 Answers 3

0

Add following element in your [app_name].exe.config file:

<connectionStrings>
    <add name="FBAContentDB" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Database=Database1;Trusted_Connection=True;MultipleActiveResultSets=true" />
</connectionStrings>

If you are using visual studio, you can:

  1. Locate your [app_name].exe.config in debug folder
  2. Copy connectionStrings section from debug config file and paste it to [app_name].exe.config file in your program's installation directory.

Also make sure, you 've SQL Server Local DB installed on your installation machine.

Sign up to request clarification or add additional context in comments.

13 Comments

Unfortunately this didn't work for me. It caused the app to crash at startup. Once I removed this, cleaned the build, and rebuilt, it worked again. :/
Is it possible for you to post your config file?
It's in the original post.
@AlfonzoAvila Check my changes, connection string I posted before was for SQL Server Db and not Local Db that is why it was crashing.
I think the reason your application is crashing because you do not have local db installed.
|
0

I had this exact same issue. It turned out to be Visual Studio was not creating an 'x86' and 'x64' directory in the bin/Debug & bin/Release directories. Each of these should have contained SNI.dll and SNI.pdb. When I copy and pasted these directories and files into the relevant directories, I had no issues.

Comments

-1

You can use the keyboard of "F11" or log to find the error location

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.