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

Entities incorrectly labeled as Added by the ChangeTracker #5442

Closed
mb236 opened this issue May 20, 2016 · 2 comments
Closed

Entities incorrectly labeled as Added by the ChangeTracker #5442

mb236 opened this issue May 20, 2016 · 2 comments

Comments

@mb236
Copy link

mb236 commented May 20, 2016

I'm having a problem after migrating my RC1 Web Application to RC2. I'll try to describe my problem as best as possible.The code did work perfectly in RC1. I'm using SQLite as provider by the way.

First I got a class A which has a virtual property of type B

public class A {
  public virtual B ObjectB { get; set;}
}

And class B has a property Id as follows

public class B {
   public int Id { get; set;} 
}

So my problem appers when I try to set the property ObjectB of an Object of Type A, to an existing object B fetched from the database. Perhaps this code snippet explains it better:

A a = new A();
a.ObjectB = b //b has been fetched from the database
db.A.Add(a); //Here the exception happens

If I try to add the new a to the database I get an DbUpdateException: {"SQLite Error 19: 'UNIQUE constraint failed: B.Id"}. This happens because the b is incorrectly labeled as Added by the ChangeTracker. But the b already exists in the Database. If I change the state to Unchanged manually the exception disappers and all runs well.

I hope my explantion was good enough. Let me know if i did anything wrong.

@mb236 mb236 changed the title Entities incorrectly labeled as Added in ChangeTracker Entities incorrectly labeled as Added by the ChangeTracker May 20, 2016
@SimonOrdo
Copy link

SimonOrdo commented May 20, 2016

I wonder if this is related to the changes to the add method's default graph behavior (#4424).

What happens if you use .Entry instead of.Add?

@mb236
Copy link
Author

mb236 commented May 22, 2016

Thanks for the tip. I solved the problem by using .Entry on every Entity that i want to add

@mb236 mb236 closed this as completed May 22, 2016
@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
None yet
Projects
None yet
Development

No branches or pull requests

3 participants