I
I
iluxa18102020-04-14 20:55:19
C++ / C#
iluxa1810, 2020-04-14 20:55:19

How to insert new objects in EF?

Let's say I have 2 contexts with the same structure and I am updating data from one context to another.

Somewhere it is updated, and somewhere it is necessary to insert completely new data from the previous context while maintaining the Id.

Actually the question is:

Let's say in the destination context there is already a User object that has a Request object, and that in turn refers to the RerquestData collection.

It turns out that I need to insert additional data to the already existing User.

Do I somehow have to notify EF that I need to do an insert, and not an update (id!=0) or is it smart and should resolve it itself?

Now, I select User from the destination context and create a new collection in the Request object, into which I copy the data from the source.

However, while saving the swearing:

Database operation expected to affect 1 row(s) but actually affected 0 row(s)

and which I wanted to insert.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-04-15
@yarosroman

If you need database replication, then for many there are ready-made solutions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question