关于 c#:在实体框架中更新多级属性

Updating multiple level properties in Entity Framework

我有一个具有多级属性的实体结构,每个头表条目都有多个子表条目,并且关系经过多级。表格图如下所示:

enter

1
2
3
4
5
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();

但我找不到将这些数据更新回数据库的方法。

当我这样尝试时:

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

我遇到了一个异常:

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.

当我尝试这个时:

1
2
3
4
5
6
7
8
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);

这就是我得到的:

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.

我尝试了此页面中建议的一些解决方案,例如添加复合键等等。它也没有用。
给出如下异常:

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

这是示例数据结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"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"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]

编辑

这就是存储库实现中的 Update 函数所做的。我该如何更改引用子表对象的状态?

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

谁能建议更新这些数据的方法?我是否需要遍历每个孩子并更新数据?在这种情况下,我也很困惑如何同时迭代来自 UI 的数据和来自数据库的实体以应用更改。我不确定我是否在搞砸一些非常简单的事情。

提前致谢。


正如@jdweng 所说,问题出在Automapper 上。
发现此链接有用。

自动映射器正在替换引用子表对象,最终将引用设置为空。它正在触发错误。