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)
}
}
}
ProcessShipmentViewModel.PopulateAmazonWarehouse()method should help us to resolve issueAppDomain.CurrentDomain.GetData("DataDirectory")to get your correct Directory and then subs your old from connectionString