1

This problem started appearing after I upgraded to Breeze 1.4.9.

I have the following entity:

public class ProjectMember
{
    public int ProjectId { get; set; }
    [ForeignKey("ProjectId")]
    [InverseProperty("ProjectMembers")]
    public Project Project { get; set; }

    public int TeamMemberId { get; set; }
    [ForeignKey("TeamMemberId")]
    [InverseProperty("ProjectMembers")]
    public TeamMember TeamMember { get; set; }
}

And its configuration:

public class ProjectMemberConfiguration : EntityTypeConfiguration<ProjectMember>
{
    public ProjectMemberConfiguration()
    {
        HasKey(a => new { a.ProjectId, a.TeamMemberId });

        // ProjectMember has 1 project, projects have many projectmember records
        HasRequired(a => a.Project)
            .WithMany(s => s.ProjectMembers)
            .HasForeignKey(a => a.ProjectId)
            .WillCascadeOnDelete(true);
    }
}

The metadata looks:

enter image description here

I create this entity on the client side with the following:

manager.createEntity('ProjectMember', { projectId: projectId, teamMemberId: teamMemberId });

All good so far, however when this entity is saved back to the server it gets duplicated on the client side as shown belo (the screenshot below shows what is in the cache after saveChanges succeeded callback is reached.

enter image description here

QUESTION Why is Breeze duplicating this entity although it should not be allowed?

EDIT

I reverted back to Breeze 1.4.8 and the problem disappeared. Here is what the manager contains after a save operation: enter image description here

GETah
  • 20,922
  • 7
  • 61
  • 103
  • What is the backend? Web API, OData (Web API? WCF)? What is processing the saveChanges on the server? THEN what is the data service adapter that you are using on the client? And the JsonResultsAdapter? You can tell what they are by stepping through the `saveChanges` method until it builds the `saveContext`. You should have access to both adapters at that point. – Ward Mar 07 '14 at 20:46
  • I am using Web API. I have a set of validation routines I run before saving the changes, these used to work just fine with Breeze 1.4.8. The data service on the client side is just a simple object I took from Jon Papa SPA application – GETah Mar 07 '14 at 21:15
  • Please note that `saveChanges` returns only one instance of `ProjectMember` so to me, it looks like a merge issue on the client side. – GETah Mar 07 '14 at 21:27

1 Answers1

1

Updated March 7 2014

This was a bug and is now fixed and available on GitHub. It will be released with a complete zip within the next few days. Any version above 1.4.9 should contain the fix.

Original post

Sorry, I can't tell from the screenshots that anything is duplicated. Are you seeing two entities in the EntityManager cache with the same key, And if so how?. Are you also seeing some form of duplication on the database as well?

Or is the problem that a 'new' entity is being created on the client after the save?

Is it possible that one part of these keys is an Identity column on the database? If so, then it's worth checking the metadata to insure that the autoGeneratedKeyType property for this EntityType is set to Identity. This would cause the database to generate a new key on insert and this entity would then be sent back to the client. Merging this entity with its previous incarnation will only occur if the AutoGeneratedKeyType is set to Identity. Otherwise, you will end up with both the original entity with the old key and a cloned version its new key.

Otherwise, I think we need more information.

Jay Traband
  • 17,053
  • 1
  • 23
  • 44
  • `ProjectMember` has a composite key: `projectId` and `teamMemberId`. As you can see in the second screenshot, there are two entities with the same keys in the cache: `projectId:16, teamMemberId:1`. Nope, the DB does not have the duplicates, EF refuses to save as the entity is duplicated. Refreshing the page fixes the issue as the cache gets cleared, but this is not an option for me. – GETah Mar 07 '14 at 16:04
  • I'll try to repro, but we have very similar tests in our test suite that succeed, so I'm a bit baffled. – Jay Traband Mar 07 '14 at 16:12
  • This is really confusing. I ran my tests again and again and I always get the same problem :(. Running `manager.getChanges()` always returns the extra `ProjectMember` entity with its state set to `Added` – GETah Mar 07 '14 at 18:18
  • I just downgraded to Breeze 1.4.8 and the problem went away, please see my edit. The problem is that I can't stick to 1.4.8 because of http://stackoverflow.com/questions/21970216/breeze-error-illegal-construction-use-or-to-combine-checks – GETah Mar 07 '14 at 18:35
  • Please note that `saveChanges` returns only one instance of `ProjectMember` so to me, it looks like a merge issue on the client side. – GETah Mar 07 '14 at 21:29
  • I am digging into it. First just trying to repro it within our test suite. – Jay Traband Mar 07 '14 at 21:47
  • Ok, I have a test that works in 1.4.8 and fails in 1.4.9. Now drilling into the cause. – Jay Traband Mar 07 '14 at 22:13
  • Ok, I've just checked in a fix to GitHub, go ahead and pull the latest version of breeze.debug.js or breeze.min.js. I'll be releasing a new nuget and zip package over the weekend... and thx for catching this. – Jay Traband Mar 08 '14 at 02:05
  • Thanks Jay. I have tested the fix, it looks good so far and the problem is gone. I will do some further testing and will get back to you. May I suggest you update your answer to point that this is a bug and has been fixed? I will then accept this as an answer :) – GETah Mar 08 '14 at 04:00