我最近来到了 ManyNavigationPropertyConfiguration 类
,在该类中我发现了一个名为 WithMany()
的方法,有 2 个重载。
第一次重载:
WithMany()
将关系配置为
许多:许多没有导航
另一边的财产
关系。
第二次过载:
WithMany(Expression>)
将关系配置为
许多:许多具有导航属性
在关系的另一边。
现在我的问题是,为什么要在没有导航属性的情况下将关系配置为“many:many”(第一个重载)?我没有看到任何有帮助的场景......有什么想法吗?
I recently came by the class ManyNavigationPropertyConfiguration<TEntity, TTarget>
, and within that class there I found a method named WithMany()
with 2 overloads.
The first overload:
WithMany()
Configures the relationship to be
many:many without a navigation
property on the other side of the
relationship.
The second overload:
WithMany(Expression<Func<TTarget, ICollection<TEntity>>>)
Configures the relationship to be
many:many with a navigation property
on the other side of the relationship.
Now is my question, why would you configure a relationship to be many:many without a navigation property (the first overload)? I dont see any scenarios where that would be helpful... Any thoughts?
发布评论
评论(2)
一个例子可能是这个模型:
如果您从来没有兴趣检索具有特定角色的所有用户,则将导航属性......添加
到
Role
类将是不必要的开销。但是您仍然必须 EF 告诉
User
和Role
之间存在多对多关系……因为默认约定映射会创建错误的关系,即一对多关系,对应这样的映射:
An example might be this model:
If you are never interested to retrieve all users which are in a specific role, adding a navigation property ...
... to the
Role
class would be unnecessary overhead.But you still must EF tell that a many-to-many relationship between
User
andRole
exists ...... because the default convention mappings would create a wrong relationship, namely a one-to-many relationship, corresponding to this mapping:
请注意,导航属性的选择位于目标的另一侧。
让我们看一个例子,尽管这个具体案例可能不是我观点的完美说明者...如果您想跟踪数学测试并重复使用问题,您可能有两个表(
Tests< /code> 和
Questions
) 具有多对多关系;每个测试有几个问题,每个问题可以出现在多个测试中。但是,您可能永远不需要获取特定问题所涉及的测试集合 - 即使您知道问题可能出现在多个测试中,但您对哪个测试不感兴趣。因此,您在声明时使用
.WithMany()
重载,这样您就可以获得一个导航属性来获取测试问题 (theTest.Questions()
),但没有另一种方式是导航属性 (theQuestion.Tests()
)。但您仍然需要多对多关系,因为测试和问题都可以有许多其他关系。我同意,在这种特定情况下,这种设置可能没有意义,但肯定有一些情况是有意义的,在这些情况下,
.WithMany()
重载让您无需定义属性(和每一个的 lambda 表达式)你永远都不需要。Note that the choice for a navigation property is on the other side of the target.
Let's look at an example, even though this specific case might not be the perfect illustrator of my point... If you want to keep track of math tests, and re-use questions, you might have two tables (
Tests
andQuestions
) which have a many-to-many relationship; each test has several questions, and each question can appear on several tests. However, you might not ever need to get a collection of tests that a specific question is on - even though you know that questions can appear on more than one test, you aren't interested in which.Thus, you use the
.WithMany()
overload when declaring this, so you get a navigational property to get the questions of a test (theTest.Questions()
) but no navigational property the other way (theQuestion.Tests()
). But you still need a many-to-many relationship, since both tests and questions can have many of the other.I agree that in this specific case this setup might not make sense, but there is certainly cases where it does, and in those cases the
.WithMany()
overload lets you get by without defining properties (and a lambda expression for each one of them) you'll never need.