0
votes

I created a WPF MVVM project in Devexpress with the Scaffolding wizard and everything created works fine, I even modified the grids so they call the SaveCommand on RowUpdated.

Now I'm trying to insert new registers programmatically and my strategy was to get and instance of the CollectionViewModel from my model and use its Save method sending an object from the same model as a parameter.

I was reading this guide but still couldn't find what i'm doing wrong.

This is the code

        Transaction transaction = new Transaction();
        transaction.IdClient = 1;

        TransactionCollectionViewModel tcvm = TransactionCollectionViewModel.Create(UnitOfWorkSource.GetUnitOfWorkFactory());
        tcvm.Save(transaction);

and it gives me this error on the variable tcvm when calling the Save function

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=DevExpress.Mvvm.v16.1.DataModel
  StackTrace:
       at DevExpress.Mvvm.DataModel.RepositoryExtensions.<>c__DisplayClass1_0`3.<GetProjectionPrimaryKey>b__0(TEntity x)
       at DevExpress.Mvvm.DataModel.RepositoryExtensions.GetProjectionValue[TEntity,TProjection,TEntityResult,TProjectionResult](TProjection value, Func`2 entityFunc, Func`2 projectionFunc)
       at DevExpress.Mvvm.DataModel.RepositoryExtensions.GetProjectionPrimaryKey[TEntity,TProjection,TPrimaryKey](IRepository`2 repository, TProjection projectionEntity)
       at DevExpress.Mvvm.DataModel.RepositoryExtensions.FindExistingOrAddNewEntity[TEntity,TProjection,TPrimaryKey](IRepository`2 repository, TProjection projectionEntity, Action`2 applyProjectionPropertiesToEntity)
       at DevExpress.Mvvm.ViewModel.CollectionViewModelBase`4.Save(TProjection projectionEntity)
       at Template.ViewModels.ClientCollectionViewModel..ctor(IUnitOfWorkFactory`1 unitOfWorkFactory) in C:\Users\edwin\Source\Workspaces\INVASST\Template\Template\ViewModels\Client\ClientCollectionViewModel.cs:line 37
       at ClientCollectionViewModel_97cd3897_6fb7_469b_9928_d61260161e61..ctor(IUnitOfWorkFactory`1 unitOfWorkFactory)
  InnerException: 

I'm using Devexpress 16.1.5 and the project is using the HybridApp template

1

1 Answers

1
votes

... my strategy was to get an instance of the CollectionViewModel from my model and use its Save method sending an object from the same model as a parameter.

I'm afraid this strategy is wrong. You should use the New() command provided by the corresponding CollectionViewModel (bound to the Grid). The real usage depends on your specific needs. In a simple case, you can just add a button above the grid and then bind the New command to this button.

Related DX support thread : Batch Insert/ Edit with Master/Detail