Abp: Does not save using '.InsertAsync(newItem)' but saves using '.InsertAsync(newItem,autosave=true)'

Created on 22 Jun 2020  路  4Comments  路  Source: abpframework/abp

Hi,

ABP Version 2.8.0

I have an entity of type MyEntity: AggregateRoot<long>

I can create default repositories and get lists of items successfully using IRepository<MyEntity, long>

But I have behaviour that does not make sense to me

If I use var newlyCreatedItem = _myRepository.InsertAsync(newItem)

  1. the new item is not saved in the database,
  2. there is no exception
  3. returned value has an Id = 0

If I use var newlyCreatedItem = _myRepository.InsertAsync(newItem, autosave: true)

  1. the new item is saved in the database,
  2. there is no exception
  3. returned value has the DB assigned Id

Why is this so? Any idea what I could've done wrong?

Thanks in advance for you help!

All 4 comments

Hi @liangshiw

Thank-you for responding, but why do you close all my questions without confirmation of resolution? Before one even reviews your answer.

I do not know if it is ABP project policy, but it seems only you do it when you respond to a question.

I have asked questions on a number of repositories and I this has to be the only situation where every question is closed before resolution. I do not believe it fosters a good spirit to close all questions when you close questions just for the sake of keeping their numbers down.

I will have a look, but thank-you.

Unfortunately, the linked documentation does not give any possible reason why it is behaving that way.

I would really appreciate if you considered my feedback concerning the way you respond and immediately close a question without considering feedback on your answer.

Sorry for bothering you, but you should check the official documentation before asking questions, because most of the answers are in the documentation.

Let me answer your question:

For EF Core, the repository operation will not be saved to the database immediately, after the method returns, it will be saved by the unit of work.

Quoted from the document

If your intent is just to save the changes after creating/updating/deleting an entity, it is suggested to use the autoSave option.
Note-1: All changes are automatically saved when a unit of work ends without any error. So, don't call SaveChangesAsync() unless you really need it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hikalkan picture hikalkan  路  3Comments

hitaspdotnet picture hitaspdotnet  路  3Comments

hikalkan picture hikalkan  路  3Comments

mehdihadeli picture mehdihadeli  路  3Comments

hikalkan picture hikalkan  路  3Comments