简体   繁体   English

更新实体框架中的多级属性

[英]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.我需要获取与 ECNEntries 表中一个条目相关的所有数据以显示在网格中。 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.附加类型为“ProductionControl.DAL.Entities.ECNEntries”的实体失败,因为相同类型的另一个实体已具有相同的主键值。 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.如果图中的任何实体具有冲突的键值,则在使用“Attach”方法或将实体状态设置为“Unchanged”或“Modified”时可能会发生这种情况。 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.这就是存储库实现中的 Update 函数所做的。 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.在这种情况下,我也很困惑如何同时迭代来自 UI 的数据和来自数据库的实体以应用更改。 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.正如@jdweng 所说,问题出在 Automapper 上。 Found this link useful.发现这个链接很有用。

The automapper was replacing the referencing child table objects, which eventually set the reference to null.自动映射器正在替换引用子表对象,最终将引用设置为 null。 it was triggering the error.它触发了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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