【问题标题】:Alternative to database for storing data?替代用于存储数据的数据库?
【发布时间】:2013-11-04 01:45:11
【问题描述】:

我在我的应用程序中使用 SQL Server Compact Edition(本地数据库)。但是当我使用实体框架时,我一直遇到一个非常烦人的 UpdateException。我的数据库只有两个小表,外键关系将它们链接在一起。所以我问是否有任何替代数据库?或者这是唯一的选择?
更新:
Here 是 EDMX 地图。
这是异常的堆栈跟踪: System.Windows.Markup.XamlParseException was unhandled Message=Cannot create instance of 'MainWindow' defined in assembly 'AssignmentOrganizer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'AssignmentOrganizer;component/MainWindow.xaml' Line 1 Position 9. Source=PresentationFramework LineNumber=1 LinePosition=9 StackTrace: at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType) at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException) at System.Windows.Markup.BamlRecordReader.ThrowExceptionWithLine(String message, Exception innerException) at System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail) at System.Windows.Markup.BamlRecordReader.GetElementAndFlags(BamlElementStartRecord bamlElementStartRecord, Object& element, ReaderFlags& flags, Type& delayCreatedType, Int16& delayCreatedTypeId) at System.Windows.Markup.BamlRecordReader.BaseReadElementStartRecord(BamlElementStartRecord bamlElementRecord) at System.Windows.Markup.BamlRecordReader.ReadElementStartRecord(BamlElementStartRecord bamlElementRecord) at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord) at System.Windows.Markup.BamlRecordReader.Read(Boolean singleRecord) at System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment() at System.Windows.Markup.TreeBuilder.Parse() at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc) at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties) at System.Windows.Application.DoStartup() at System.Windows.Application.<.ctor>b__0(Object unused) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter) at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.Run() at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at AssignmentOrganizer.App.Main() in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\AssignmentOrganizer\AssignmentOrganizer\obj\x86\Debug\App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.Reflection.TargetInvocationException Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail) InnerException: System.Data.UpdateException Message=An error occurred while updating the entries. See the InnerException for details. Source=System.Data.Entity StackTrace: at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave) at System.Data.Objects.ObjectContext.SaveChanges() at AssignmentOrganizer.App_Data.AssignmentRepository.CreateAssignment(Assignment assignmentToCreate) in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\AssignmentOrganizer\AssignmentOrganizer\App_Data\AssignmentRepository.cs:line 20 at AssignmentOrganizer.MainWindow..ctor() in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\AssignmentOrganizer\AssignmentOrganizer\MainWindow.xaml.cs:line 34 InnerException: System.Data.EntityCommandCompilationException Message=An error occurred while preparing the command definition. See the inner exception for details. Source=System.Data.Entity StackTrace: at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree) at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary2 标识符值) 在 System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator 翻译器,EntityConnection 连接,Dictionary2 identifierValues, List1 generatedValues) 在 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter 适配器) 内部异常:System.NotSupportedException Message=SQL Server Compact 不支持服务器生成的键和服务器生成的值。 源=System.Data.SqlServerCe.Entity 堆栈跟踪: 在 System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateReturningSql(StringBuilder commandText,DbModificationCommandTree 树,ExpressionTranslator 转换器,DbExpression 返回) 在 System.Data.SqlServerCe.SqlGen.DmlSqlGenerator.GenerateInsertSql(DbInsertCommandTree 树,List1& parameters, Boolean isLocalProvider) at System.Data.SqlServerCe.SqlGen.SqlGenerator.GenerateSql(DbCommandTree tree, List1& 参数,CommandType& commandType,布尔 isLocalProvider) 在 System.Data.SqlServerCe.SqlCeProviderServices.CreateCommand(DbProviderManifest providerManifest,DbCommandTree commandTree) 在 System.Data.SqlServerCe.SqlCeProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest,DbCommandTree commandTree) 在 System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree 命令树) 在 System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree) 在 System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree 命令树) 内部异常: `

【问题讨论】:

  • 您能否举例说明您在两个表中存储的数据类型?我会说 XML 文件,但最好先看看一些数据示例,然后再回答。
  • 找出导致 UpdateException 的原因并修复它而不是转储数据库不是更好吗?
  • 我的更新能回答你的问题吗?

标签: c# database


【解决方案1】:

总是有替代方案。问题是它们是否比数据库更好。您可以将数据存储为 XML 或您自己的文件格式等。然后您必须编写适当的查询、更新代码等。可能没问题 - 但我们不能说没有更多信息。

但是,在考虑其他解决方案之前,我会深入了解您看到异常的原因。当您更改存储层时,您很可能会遇到一些您没有真正预料到的行为,这些行为同样会伤害您——而且那时可能更难诊断。一旦你弄清楚了为什么你当前的方法不起作用,你就会更好地知道它是否值得改变。

【讨论】:

    【解决方案2】:

    如果您没有太多数据,XML 文件也可以使用。很难说,如果没有更多的系统知识。

    【讨论】:

      【解决方案3】:

      嗯,使用关系数据库的主要原因是确保数据完整性和关系

      如果你可以不使用外键约束,只需将其存储为平面文件(excel、xml 等...)

      【讨论】:

        【解决方案4】:

        SQLite 也是一个不错的选择。无需安装服务器。

        【讨论】:

        • SQL Server Compact Edition 也不需要服务器。与 SQLite 一样,它在进程内运行。
        • 实际的 SQL Server(基于服务的数据库)不需要安装 SQL Server 吗?我想?
        【解决方案5】:

        您选择数据库实现可能是有充分理由的,特别是在关系和完整性很重要的情况下。尝试找出您遇到异常的原因 - 如果它是 SQL CE 的事情,那么您可以考虑像 ESE (Jet) 这样的替代方案,它是所有 Windows 安装的一部分,并且更加强大。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-27
          • 2011-02-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多