Entity Framework Code first - Specify minimum one cardinality in a many to many relation
Is there a way in code first to specify in a many to many relation that minimum one entity must exist?
C#.NET ProgrammingMicrosoft Development
Last Comment
itnifl
8/22/2022 - Mon
AndyAinscow
Could you have an inbetween table that had 1:n relations to the other two tables ?
itnifl
ASKER
Perhaps? If it works?
AndyAinscow
In database design that is a typical method for n:n relations.
A possible (not so good) example. You have multiple invoices and multiple quotes in two tables. A third table would be customers - one customer can have many invoices and that same one customer can have many quotes.
Sure, but in my case I need to restrict the Many to Many relation via Entity Framework to never allow one entity without at least one other entitity, a cardinality of minimum 1. And my question is, how can I practically in example do that?
AndyAinscow
>>never allow one entity without at least one other entitity
I don't understand what you mean by that
itnifl
ASKER
This relation:
Many to many but never less than one.
Person >|--|< Bills
Still don't understand what you mean with never less than one.
With a 1:n relation you need a record in the master table before you can add records to the child table.
itnifl
ASKER
Here you see a Certificate and System.
A System can have zero or many certificates.
A Certificate can have one or many Systems, but never zero.
1:n is a one to many relationship. With an intermediate table as you say, you can have 1:n relationships to two other tables, creating a many to many relation between those tables through the intermediate table. But is the cardinality enforced?
, but I must enforce that zero entities is never allowed. So I want a Student to have minimum one course, and never allow the student to have zero Courses.