添加迁移命令无法正常工作错误0x80073D54

时间:2016-09-11 13:09:12

标签: c# entity-framework migration uwp

我通过https://blogs.windows.com/buildingapps/2016/05/03/data-access-in-universal-windows-platform-uwp-apps/#LTm66jG1JvVVXDxi.97

创建了一个应用程序

但是命令添加迁移MyFirstMigration 不起作用。 你知道为什么

PM> Add-Migration MyFirstMigration
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: **The process does not contain the identity of the package (Exception from HRESULT: 0x80073D54)**
   at Windows.Storage.ApplicationData.get_Current()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Data.Sqlite.SqliteConnection.GetApplicationData()
   at Microsoft.Data.Sqlite.SqliteConnection.AdjustForRelativeDirectory(String path)
   at Microsoft.Data.Sqlite.SqliteConnection.Open()
   at Microsoft.Data.Entity.Storage.RelationalConnection.Open()
   at Microsoft.Data.Entity.Storage.Internal.SqliteRelationalConnection.Open()
   at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.Execute[T](IRelationalConnection connection, Func`3 action, String executeMethod, Boolean openConnection, Boolean closeConnection)
   at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.ExecuteScalar(IRelationalConnection connection, Boolean manageConnection)
   at Microsoft.Data.Entity.Storage.Internal.SqliteDatabaseCreator.HasTables()
   at Microsoft.Data.Entity.Storage.RelationalDatabaseCreator.EnsureCreated()
   at Microsoft.Data.Entity.Infrastructure.DatabaseFacade.EnsureCreated()

1 个答案:

答案 0 :(得分:0)

我无法重现您的问题。我的环境信息如下:Visual Studio 2015更新3,Windows 10 SDK 14393,UWP目标版本14393,最低版本10586.

但根据您的异常堆栈详细信息,Windows.Storage.ApplicationData.get_Current()发生错误。似乎添加迁移操作想要访问您应用的本地文件夹,但由于未部署该应用,因此目前没有本地文件夹。

Add-Migration将实例化您的DbContext类,该类检索Windows.Storage.ApplicationData.Current.LocalFolder.Path的值以构建连接字符串。由于它是从VS调用的,因此在Windows应用商店应用的上下文中运行时缺少提供的包标识符似乎失败了。

因此,请尝试部署您的应用或首先运行它以确保本地文件夹存在。如果已创建本地文件夹,但异常仍然存在,则可以像下面那样对详细信息连接字符串进行硬编码,而不是仅编写数据库文件名Sensors.db(OnConfiguring方法在模型类中)。以下代码中的文件夹名称61bbd292-ef8c-4a2e-b0c4-360dd2335d4d_9889v3km7snsp是您应用的软件包系列名称,您可以在Package.mainfest - > Packaging中找到。

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 {
     var connection = @"Filename=C:\Users\YourUserName\AppData\Local\Packages\61bbd292-ef8c-4a2e-b0c4-360dd2335d4d_9889v3km7snsp\LocalState\Sensors.db";
     optionsBuilder.UseSqlite(connection);
 }

更多详情请参阅实体框架issue1058中的第7层。