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

Issues updating many-to-many owned collections #137

Open
cryo75 opened this issue Apr 17, 2015 · 2 comments
Open

Issues updating many-to-many owned collections #137

cryo75 opened this issue Apr 17, 2015 · 2 comments

Comments

@cryo75
Copy link

cryo75 commented Apr 17, 2015

I have this model:

public class Customer
{
    public int Id {get; set;}
    public int Name {get; set;}
    public virtual ICollection<CustomerCategory> Categories {get; set;}
} 

public class CustomerCategory
{
    public int Id {get; set;}
    public int CustomerId {get; set;}
    public int CategoryId{get; set;}
    public virtual Customer Customer {get; set;}
    public virtual Category Category {get; set;}
} 

public class Category
{
    public int Id {get; set;}
    public int Name {get; set;}
} 

The relationship mapping for the Customer is:

internal class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap ()
    {
        this.HasMany(t => t.Categories)
            .WithOptional()
            .HasForeignKey(d => d.CustomerId)
            .WillCascadeOnDelete();
   }
}

For adding and updating I call the same method:

    public Customer Update(Customer entity)
    {
        foreach (CustomerCategory category in entity.Categories)
        {
            category.CustomerId = entity.Id;
            category.CategoryId = category.Category.Id;
        }

        return ((DbContext)dataContext).UpdateGraph<Customer >
            (entity,
                map => map.OwnedCollection(x => x.Categories)
            );
    }

NOTE:

  1. I'm not using the default naming convention for primary and foreign keys in the database.
  2. My DTO classes are similar to the above models but they do not contain the foreign key properties. When mapping, these are ignored by AutoMapper.

There are a few issues with GraphDiff:

  1. When adding new categories to the customer, without the foreach loop in the Update method, GraphDiff sends insert statements where customerId and categoryId are 0. Thefore the inserts fail.
  2. After the entity is updated, GraphDiff returns the updated customer, but the Category propery of each CustomerCategory is null.

Why is this happening? Am I missing something or is this a limitation of GraphDiff and/or EF?

@ilmax
Copy link

ilmax commented Jul 13, 2015

Hi, I think that actually this is a limitation of graphdiff due to a BUG, i will try to submit a pull request to fix this in the near future, btw you could add FK to your DTOs, this should solve the issue.

@ghost ghost added the potential bug label Jul 22, 2015
@rwdalpe
Copy link

rwdalpe commented Sep 29, 2015

I can confirm that I have also seen this behavior where

  • You are saving a parent object with an OwnedCollection that is creating new child objects
  • The child objects on the OwnedCollection have FK references back to the parent object
  • The FK references from the child to the parent are not assigned values
  • When you attempt to save the parent, EF throws an error about the EntityKey of the child object not being valid.

As described by cryo75, the workaround is to give values to the child objects' FKs to the parent object. I've not done sufficient testing to know yet whether or not this is a bug in GraphDiff/GraphDiff's usage of EF, or a problem with EF itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants