Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it possible to add a new entity as added but all related entities as unchanged? #9185

Closed
ComptonAlvaro opened this issue Jul 15, 2017 · 1 comment
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@ComptonAlvaro
Copy link

ComptonAlvaro commented Jul 15, 2017

i am trying to add an entity as added state but I want that all the related entites are added recursivelly as unchanged.

I have tried:

dbContext.Entry(myEntity).State = EntityState.Added;

This add the entity as added but recursively all the related entites are added as added too.

dbContext.Attached(myEntity);

This add the entity as unchanged, so I have to chaged its status to add. All related entities are added as unchenged.

dbContex.MyType.Add(myEntity);

Add all the entities as added status.

dbContext.Update(myEntity) add my entity as modified, but I need to add as added.

So I don't know if it is possible to add one entity as added and rescursive all the reset as unchanged.

The entity that I am trying to add is a realtion entity:

MyEntity
{
long IDTableA;
long IDTableB;
}

IDTableA and IDTableB are not 0, so update adds it as modified. But I know that it doesn't exists (it is not a multiuser application so only one user uses the database) so I wanted to add it as added entity. Perhaps it would be posssible to add an entity with IDs as added because I know that It doens't exist and add the related entities as unchanged.

Thanks.

@ajcvickers
Copy link
Member

@ComptonAlvaro If you are using generated keys and the new entity does not have a key value set, then Attach should work in EF Core 2.0. In other cases, or when using EF Core 1.1, use TrackGraph.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Jul 17, 2017
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

2 participants