简体   繁体   中英

Updating multiple level properties in Entity Framework

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:

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM