You can use Metadata property to get the associated INavigation, then ForeignKey property to get the associated IForeignKey, then the Properties property to get the associated properties of type IProperty, and finally the Name property which can be passed to Property method to obtain the associated PropertyEntry for manipulating the value.
采取行动:
var entry = dbContext.Entry(entity);
foreach (var reference in entry.References)
{
reference.CurrentValue = null;
foreach (var fkProperty in reference.Metadata.ForeignKey.Properties)
entry.Property(fkProperty.Name).CurrentValue = null;
}
<>Update: 在最近的核心版本中,<代码>Metadata 房地产类型已改为INavigation 基地代码>(可能作为准备航道性能支助的一部分)没有<编码>ForeignKey
财产,因此,需要人工将其投到上述<代码>INavigation接口,例如:
using Microsoft.EntityFrameworkCore.Metadata;
...
foreach (var fkProperty in ((INavigation)reference.Metadata).ForeignKey.Properties)
...