【发布时间】:2019-04-05 01:03:08
【问题描述】:
使用 EF 6 和 Devart Entity Design 和 DotConnect for SQLite。在调试、发布和安装后运行时,嵌入式数据库位置存在问题。
将数据库文件复制到 bin/Debug 或 bin/release 并使用以下方式访问数据库就可以了:
Data Source=database.db
在我的连接字符串中。当我在调试时运行时,我可以很好地访问数据库。如果我安装,我无法通过读/写访问数据库 - 只是只读。
所以...我已经让应用程序确保数据库(以及一些其他资源在用户目录\AppData\Roaming 中可用,方法是在表单加载时运行它:
string basePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ThermalVision\";
//Later...
if(!File.Exists(basePath + "database.db"))
{
string source = AppDomain.CurrentDomain.BaseDirectory + "database.db";
File.Copy(source, basePath + "database.db");
}
在 Visual Studio 中以调试模式运行时也可以正常工作。
我的元数据连接字符串目前看起来像这样(代码由 Entity Developer 生成):
public Entities() :
base(@"metadata=DataModel1.csdl|DataModel1.ssdl|DataModel1.msl;provider=Devart.Data.SQLite;provider connection string=""Data Source=C:\Users\erics\AppData\Roaming\ThermalVision\database.db;FailIfMissing=False""", "Entities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
安装此应用并启动时,我收到以下错误:
System.Data.Entity.Core.MetadataException:指定的元数据路径 无效。有效路径必须是现有目录、 扩展名为“.csdl”、“.ssdl”或“.msl”或 URI 的现有文件 标识嵌入式资源。在 System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader.Create(字符串 路径,ExtensionCheck extensionCheck,字符串validExtension, ICollection
1 uriRegistry, MetadataArtifactAssemblyResolver resolver) at System.Data.Entity.Core.Metadata.Edm.MetadataCache.SplitPaths(String paths) at System.Data.Entity.Core.Common.Utils.Memoizer2.c__DisplayClass2.b__0() 在 System.Data.Entity.Core.Common.Utils.Memoizer2.Result.GetValue()2.Evaluate(TArg arg) 在 System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetArtifactLoader(DbConnectionOptions 有效连接选项)在 System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetMetadataWorkspace(DbConnectionOptions 有效连接选项)在 System.Data.Entity.Core.EntityClient.EntityConnection.GetMetadataWorkspace() 在 System.Data.Entity.Core.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection() 在 System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection 连接,布尔 isConnectionConstructor, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory, Translator 翻译器,ColumnMapFactory columnMapFactory) 在 System.Data.Entity.Core.Objects.ObjectContext..ctor(字符串 连接字符串,字符串默认容器名称)在 Model.Entity..ctor() 在 ThermalVision.Form1.SetMachineTextBoxOptions() 在 ThermalVision.Form1.Form1_Load(Object sender, EventArgs e) at System.Windows.Forms.Form.OnLoad(EventArgs e) 在 System.Windows.Forms.Form.OnCreateControl() 在 System.Windows.Forms.Control.CreateControl(布尔 fIgnoreVisible)
at System.Data.Entity.Core.Common.Utils.Memoizer
在 System.Windows.Forms.Control.CreateControl() 在 System.Windows.Forms.Control.WmShowWindow(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WmShowWindow(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
我不确定如何解决这个问题。我的路径是硬编码的,当我在 Entity Developer 中测试时,数据库连接得很好。
这里的另一个问题似乎是,当资源在 Visual Studio 中的调试模式下使用资源时,以及在安装应用程序时在产品中使用时,如何处理读/写嵌入式资源?
【问题讨论】:
标签: c# entity-framework-6 devart