0

I have an entity structure having multiple level of properties, each header table entry has multiple child table entries, and the relation goes through multiple level. Table diagram is shown here:

enter image description here

I need to get all data related to one entry in ECNEntries table to display in a grid. The user may edit the entries, delete some, add more entries at all levels.

I can get all the data for a particular entry like this:

var ECNEntryInDb = ECNEntriesRepo.FindAll(f => f.ECNStages
                                .Select(i => i.ECNComponentDetails
                                    .Select(j => j.ECNMaterialReferenceDetails
                                        .Select(k => k.ECNComponentDetails))))
                                        .Where(x => x.Id == ECNEntriesData.Id).FirstOrDefault();

But I can't find a way to update this data back to the database.

When I tried like this:

var xx = Mapper.Map<DAL.Entities.ECNEntries>(ECNEntriesData);
ECNEntriesRepo.Update(xx);

I'm getting an exception:

Attaching an entity of type 'ProductionControl.DAL.Entities.ECNEntries' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

And when I tried this:

var ECNEntryInDb = ECNEntriesRepo.FindAll(f => f.ECNStages
                                    .Select(i => i.ECNComponentDetails
                                        .Select(j => j.ECNMaterialReferenceDetails
                                            .Select(k => k.ECNComponentDetails)))).Where(x => x.Id == ECNEntriesData.Id).FirstOrDefault();
                                //var ECNEntryInDb = ECNEntriesRepo.FindById(ECNEntriesData.Id);

Mapper.Map<BL.DTO.ECN.ECNEntries, DAL.Entities.ECNEntries>(ECNEntriesData, ECNEntryInDb);
ECNEntriesRepo.Update(ECNEntryInDb);

This is what I get:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

I tried some solutions suggested in this page like adding composite keys and all. It didn't work either. Gives an exception like:

Adding a relationship with an entity which is in the Deleted state is not allowed

This is the sample data structure:

"EcnEntries": [
                {
                    "Id": 49,
                    "ECNHeaderId": 11,
                    "ECNVersionId": 8,
                    "ECNVersion": null,
                    "ECNPartNumber": "280384-01/01M",
                    "ECNStages": [
                        {
                            "Id": 25,
                            "ECNEntriesId": 49,
                            "OperationNo": "006",
                            "WorkCenterId": 198,
                            "ChangesRequired": "asxa",
                            "ReasonForChanges": "erte",
                            "ECNComponentDetails": [
                                {
                                    "Id": 31,
                                    "ECNStagesId": 25,
                                    "CurrentBOMPartNumber": "jhjhk",
                                    "ReplacementComponentPartnumber": "uyuyuy",
                                    "ECNMaterialReferenceDetails": [
                                        {
                                            "Id": 38,
                                            "ECNComponentDetailsId": 31,
                                            "MaterialReferenceNumber": "323"
                                        },
                                        {
                                            "Id": 39,
                                            "ECNComponentDetailsId": 31,
                                            "MaterialReferenceNumber": "12"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]

EDIT

This is what the Update function in the repository implementation does. How can i change the status of the referencing child table objects also?

public void Update(T entity)
        {
            contextFactory.GetDataContext().Entry(entity).State = EntityState.Modified;
        }

Can anyone suggest a way to update this data? Do I need to iterate through each child and update the data? In that case also I'm confused how to iterate through the data coming from UI and the entity from the database simultaneously to apply the change. I'm not sure if I'm messing up something which is really simple.

Thanks in advance.

  • Is the Mapper.Map instruction failing? I don't think it is needed. – jdweng Mar 06 '20 at 08:18
  • I think the problem is with maintaining the status of the entities. But i don't know how to change the status of referencing child table entries. – Kiran Sankar Mar 06 '20 at 10:14
  • When a query is done using Entity there is a property ISChanged which is set to false for each object. When an object get changed the IsChanged property is set true. Then when an update is done all the IsChanged values are updated in the database. – jdweng Mar 06 '20 at 11:44
  • @jdweng So, if i have a data-set having reference to child objects, And have updated some property values in the child objects also, then on updating the object, the child object status is also changed? It doesn't seems to happen here. Can i explicitly set the statuses of the all the changed child objects someway?And in my case there can be more than one level of parent-child relationships as given in the above sample data.So should i loop through each level and change the property values? – Kiran Sankar Mar 06 '20 at 13:09
  • What type of database are you using? – jdweng Mar 06 '20 at 13:47
  • sql server 2012. – Kiran Sankar Mar 07 '20 at 15:41

1 Answers1

0

As @jdweng said, the problem was with Automapper. Found this link useful.

The automapper was replacing the referencing child table objects, which eventually set the reference to null. it was triggering the error.