WPF应用程序在调试中运行,单击按钮后部署时崩溃

时间:2018-08-21 23:13:00

标签: c# .net wpf entity-framework-6 clickonce

我已经在VS17中创建了WPF应用程序。我将实体框架代码优先版本6.0用于数据。我还使用了一些第三方库,例如Newtonsoft.Json和EPPlus。我通过NuGet命令行安装了所有这些工具。

当我在其中运行应用程序(带有和不带有调试)时,它按预期运行得很好。我使用简单的ClickOnce方法发布了该应用程序。该应用程序已发布,我可以对其进行设置。该应用程序启动正常。单击按钮转到应用程序的一部分后,应用程序崩溃。

我查看了Windows事件查看器,并且知道它与使用Entity Framework部署数据库有关。这是事件日志

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()

此外,这是我的App.config文件。

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

这是我的DbContext类

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; }

}

我看过的this post听起来很相似,但是我的应用程序实际上正在启动,然后移到要查询数据库的部分时崩溃。

我还应该提到我使用了Migrations(不确定是否很重要)。在Configuration.cs中,我唯一覆盖的方法是Seed()方法来填充起始数据。

有人可以指出正确的方向吗?

如果您需要其他信息,请让我。我感谢任何愿意提供帮助的人。

编辑/更新1 根据{{​​3}}的建议,添加连接字符串无效。它导致应用程序在调试模式下崩溃。根据{{​​3}},这是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 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

我遇到了同样的问题。原来是Visual Studio没有在bin / Debug和bin / Release目录中创建“ x86”和“ x64”目录。其中每个都应包含SNI.dll和SNI.pdb。当我将这些目录和文件复制并粘贴到相关目录中时,没有任何问题。

答案 2 :(得分:-1)

您可以使用“ F11”键盘或登录以查找错误位置