2010-02-09 120 views
1

我爲我的應用程序使用了SQL Server Compact Edition(本地數據庫)。但是,當我使用實體框架時,我經常遇到非常煩人的UpdateException。我的數據庫只有兩個帶有外鍵關係的小表連接在一起。所以我問是否有任何數據庫的替代方案?或者是唯一的選擇?
更新:
Here是EDMX地圖。
下面是異常的堆棧跟蹤:在System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute 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, Dictionary 2 identifierValues) (UpdateTranslator翻譯,EntityConnection連接,字典2 identifierValues, List 1個generatedValues) 在System.Data.Mapping。 Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter適配器) InnerException:System.NotSupportedException 消息= 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樹,列表參數,的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 commandTree) 在System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree) 在System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree) InnerException: `用於存儲數據的數據庫的替代方法?

+0

你可以舉一個你存儲在兩個表中的數據類型的例子嗎?我會說XML文件,但最好在跳回答之前查看一些數據示例。 – 2010-02-09 17:54:16

+0

找出引起_UpdateException_的原因並解決這個問題不是更好嗎? – 2010-02-09 18:34:58

+0

我的更新是否回答你的問題? – 2010-02-09 23:57:09

回答

10

總有一些替代品。問題是他們是否比數據庫好。您可以將數據存儲爲XML或自己的文件格式等。然後,您必須編寫適當的查詢,更新代碼等。可能沒問題 - 但我們不可能說沒有更多信息。

但是,即使在考慮另一種解決方案之前,我也會深究爲什麼會看到異常。你可能會有一些行爲你沒有真正預料到,當你改變了存儲層的時候,這些行爲同樣會傷害到你,而且在那時可能更難診斷。一旦你弄清楚了你目前的方法爲什麼不起作用,你就會更好地瞭解它是否值得改變。

1

如果您的數據量不是太多,那麼XML文件將起作用。很難說,但沒有更多的系統知識。

1

那麼使用關係數據庫的主要原因是爲了確保數據的完整性和關係

如果你生活中可以沒有外鍵約束,只是把它存儲爲平面文件(EXCEL,XML等)

1

SQLite也是一個不錯的選擇。無需安裝服務器。

+1

SQL Server Compact Edition也不需要服務器。像SQLite一樣,它在進程中運行。 – 2010-02-09 18:01:47

+0

實際的SQL Server(基於服務的數據庫)是否要求您安裝SQL Server?我認爲? – 2010-02-10 18:12:34

1

您選擇數據庫實施可能有一個很好的理由,特別是如果關係和完整性很重要。試着弄清楚爲什麼你會遇到異常 - 如果它是一個SQL CE的東西,那麼你可以考慮像ESE(Jet)這樣的替代品,它是所有Windows安裝的一部分,並且更加健壯。

相關問題