如何确定数据库条目的集合是否是多对多关系的一部分?
How can I determine whether a db entry's collection is part of a many-to-many relationship?
当遍历 Db 条目的成员时,我想确定特定导航是否是多对多关系的一部分
调试的时候可以看到一个属性 collection.Metadata.ManyToManyLoader
:
它似乎只出现在我的多对多集合中
但是,我似乎无法在我的代码中定位或 select 属性:
var test = collection.Metadata["ManyToManyLoader"];
var test2 = collection.Metadata.FindAnnotation("ManyToManyLoader");
上面的 test
和 test2
都只是 null
,尽管我可以看到它们不应该出现在我的本地选项卡中
什么给了?
更多代码:
var entry = DbContext.Entry(entity);
foreach (var collection in entry.Collections)
{
var test = collection.Metadata["ManyToManyLoader"];
var test2 = collection.Metadata.FindAnnotation("ManyToManyLoader");
}
从 EF Core 5.0 开始,集合条目的 Metadata
属性 为 INavigationBase
。这实际上可以是 INavigation
(对于“常规”集合导航)或 ISkipNavigation
(对于当前用于隐式 many-to-many 关系的跳过导航)。
因此您可以检查该接口,例如
if (collection.Metadata is ISkipNavigation info)
{
// some additional info if needed
var declaringEntityType = info.DeclaringEntityType; // the entity containing the navigation
var targetEntityType = info.TargetEntityType; // "other side" entity
var joinEntityType = info.JoinEntityType; // join entity
var foreignKey = info.ForeignKey; // foreign key of join entity to this entity
var inverse = info.Inverse; // the navigation property of the "other side" entity
}
等等
当遍历 Db 条目的成员时,我想确定特定导航是否是多对多关系的一部分
调试的时候可以看到一个属性 collection.Metadata.ManyToManyLoader
:
它似乎只出现在我的多对多集合中
但是,我似乎无法在我的代码中定位或 select 属性:
var test = collection.Metadata["ManyToManyLoader"];
var test2 = collection.Metadata.FindAnnotation("ManyToManyLoader");
上面的 test
和 test2
都只是 null
,尽管我可以看到它们不应该出现在我的本地选项卡中
什么给了?
更多代码:
var entry = DbContext.Entry(entity);
foreach (var collection in entry.Collections)
{
var test = collection.Metadata["ManyToManyLoader"];
var test2 = collection.Metadata.FindAnnotation("ManyToManyLoader");
}
从 EF Core 5.0 开始,集合条目的 Metadata
属性 为 INavigationBase
。这实际上可以是 INavigation
(对于“常规”集合导航)或 ISkipNavigation
(对于当前用于隐式 many-to-many 关系的跳过导航)。
因此您可以检查该接口,例如
if (collection.Metadata is ISkipNavigation info)
{
// some additional info if needed
var declaringEntityType = info.DeclaringEntityType; // the entity containing the navigation
var targetEntityType = info.TargetEntityType; // "other side" entity
var joinEntityType = info.JoinEntityType; // join entity
var foreignKey = info.ForeignKey; // foreign key of join entity to this entity
var inverse = info.Inverse; // the navigation property of the "other side" entity
}
等等