0

ObjectContext.SaveChanges メソッドに問題があります。

  public void Save(Action<object, object> action)
    {
        EventAggregator.GetEvent<EntityServiceRequestingEvent>().Publish(true);
        var bw = new BackgroundWorker();
        var saved = false;
        bw.DoWork += (o, ee) =>
                         {
                             ProgramStatusService.Status = Resources.DatabaseSavingMessage;


                             if (!CanSave())
                             {
                                 throw new InvalidOperationException(
                                     "You must not call Save when CanSave returns false.");
                             }
                             try
                             {
                                 lock (Context)
                                 {
                                     Context.SaveChanges();
                                     saved = true;    
                                 }
                             }
                             catch (ValidationException e)
                             {
                                 MessageService.ShowError(null, string.Format(CultureInfo.CurrentCulture,
                                                                              Resources.SaveErrorInvalidEntities,
                                                                              e.Message));
                             }
                             catch (UpdateException e)
                             {
                                 var innerException = e.InnerException as SqlException;
                                 if (innerException != null && innerException.Number == 2601)
                                 {
                                     MessageService.ShowError(null, string.Format(CultureInfo.CurrentCulture,
                                                                                  Resources.DublicateKeyUpdateError,
                                                                                  e.InnerException.Message));
                                 }
                                 else
                                 {
                                     MessageService.ShowError(null, string.Format(CultureInfo.CurrentCulture,
                                                                                  Resources.SaveErrorInvalidFields,
                                                                                  e.InnerException.Message));
                                 }
                             }
                             ee.Result = saved;
                         };
        bw.RunWorkerCompleted += (o, e) =>
                                     {
                                         EventAggregator.GetEvent<EntityServiceRequestingEvent>().Publish(false);
                                         ProgramStatusService.Status = Resources.ProgramReadyMessage;
                                         if (e.Result != null) action.Invoke(this, e.Result);
                                     };
        bw.RunWorkerAsync();
    }

保存中にエラーが発生しました:別のスレッドが所有しているため、呼び出し元のスレッドはこのオブジェクトにアクセスできません。Designer.cs で:

   /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public Nullable<global::System.DateTime> MDate
    {
        get
        {
            return _MDate;
        }
        set
        {
            OnMDateChanging(value);
            ReportPropertyChanging("MDate");
            _MDate = StructuralObject.SetValidValue(value);
     ReportPropertyChanged("MDate");// **ERROR HERE**

            OnMDateChanged();
        }
    }

BackgroundWorkerでObjectContextを適切に使用するには?

4

1 に答える 1

5

バックグラウンドワーカーでコンテキストを使用する唯一の正しい方法は、ハンドラーでコンテキストを作成し、それを使用して、ハンドラーが終了DoWorkする前に破棄することです。はスレッドセーフではなく、ロックしても変更されません。DoWorkObjectContextSaveChanges

受信している例外は、EF が原因ではなく、WPF と UI コントロールが原因です。非 UI スレッドからコントロールを変更することはできません。コントロールを操作するには、Dispatcher を使用する必要があります。

于 2012-06-25T12:13:53.697 に答える