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?
LVL 2
itniflAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Could you have an inbetween table that had 1:n relations to the other two tables ?
0
itniflAuthor Commented:
Perhaps? If it works?
0
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

itniflAuthor Commented:
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?
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>never allow one entity without at least one other entitity

I don't understand what you mean by that
0
itniflAuthor Commented:
This relation:
Many to many but never less than one.
 Person >|--|< Bills
0
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
itniflAuthor Commented:
ERHere 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?

Actually, in Entity Framework something like that is automatically generated when I use http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

But I can via C# in entity framework still have zero entities in lets say:
public virtual ICollection<Student> Students { get; set; }

Open in new window

, 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.

So what I want is Max Cardinality via Entity Framework. http://stackoverflow.com/questions/5104349/what-is-the-difference-between-max-cardinality-and-min-cardinality

Can it be enforced?
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>Can it be enforced?

That I don't know.  I have limited experience with Entity but I have a lot of experience of databases.  With a database I'd always use an intermediate table if I required n:n to make it n:1:n in the relationships.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itniflAuthor Commented:
OK, thank you. Believing that there is no way to do it in Entity Framework, because there is no source on it anywhere.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.