通过消息服务传输一个实体对象,改变它并将其附加回来。

问题描述 投票:0回答:1

我通过MassTransit传输一个实体对象,更改一些属性并将其返回。当然这涉及到后台的序列化deserialization,由MassTransit使用RabbitMQ自动完成。

当我尝试附加或更新实体

_machinesContext.Machines.Update((Machine)machine);

它抛出一个期望值,即密钥丢失。

ex = {"Value cannot be null. (Parameter 'key')"}

at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)\r\n   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)\r\n   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityReferenceMap.TryGet(Object entity, IEntityType entityType, InternalEntityEntry& entry, Boolean throwOnNonUniqueness)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.TryGetEntry(Object entity, IEntityType entityType, Boolean throwOnTypeMismatch)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, Boolean fromQuery)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.FireStateChanged(EntityState oldState)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode`1 node)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)\r\n   at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)\r\n   at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)\r\n   at Microsoft.EntityFrameworkCore.DbContext.Update[TEntity](TEntity entity)\r\n   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Update(TEntity entity)\r\n

只是想说一下,主键被称为 .MachineId 它不是空的,它有一个 int 值。

我发现更新对象的唯一方法就是通过 .Find,然后用 .CurrentValues.SetValues(transferedObject) 像这样。

var existingMachine = await _machinesContext.Machines.FindAsync(machine.MachineId);
_machinesContext.Entry(existingMachine).CurrentValues.SetValues((Machine)machine);
await _machinesContext.SaveChangesAsync();

有没有更好的方法来附加和更新实体?

c# entity-framework entity-framework-core masstransit
1个回答
0
投票

更新方法 它把所有的属性都放在一个查询中,即使没有对它们进行修改.在后台,你的对象集的状态修改.你可以通过反射搜索所有的属性,只修改那些已经改变的。

© www.soinside.com 2019 - 2024. All rights reserved.