简体   繁体   中英

Entity Framework specify cardinality in a many to many relationship

We have a many-to-many relationship, which we specify fluently like this:

Hasmay(A=>A.Bs).WithMany(B.As)

An "A" can have zero or more "B"'s. An "B" must have one or more "A"'s.

Is there a way to specify the cardinality for the "As" in "B"?

No, because under the hood it's a 1-n-1 association. That is, A has zero, one, or many AB junction records, and AB always has one B (and always one A ).

So we're dealing with the rules of 1-n associations. There is no way to enforce a 1 - 1 ..n association (ie a parent must have at least one child), at least not in DDL. Consequently there is no way to enforce 1..n - 1..m associations. It's always 0..n - 0..m. Only by business logic you can enforce mandatory associations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM