Solved

Entity Framework Code first - Specify minimum one cardinality in a many to many relation

Posted on 2016-09-06
10
56 Views
Last Modified: 2016-09-11
Is there a way in code first to specify in a many to many relation that minimum one entity must exist?
0
Comment
Question by:itnifl
  • 5
  • 5
10 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41786678
Could you have an inbetween table that had 1:n relations to the other two tables ?
0
 
LVL 2

Author Comment

by:itnifl
ID: 41787447
Perhaps? If it works?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41787454
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
 
LVL 2

Author Comment

by:itnifl
ID: 41787558
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41787838
>>never allow one entity without at least one other entitity

I don't understand what you mean by that
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 2

Author Comment

by:itnifl
ID: 41792839
This relation:
Many to many but never less than one.
 Person >|--|< Bills
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41792873
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
 
LVL 2

Author Comment

by:itnifl
ID: 41793166
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
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 41793181
>>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
 
LVL 2

Author Closing Comment

by:itnifl
ID: 41793485
OK, thank you. Believing that there is no way to do it in Entity Framework, because there is no source on it anywhere.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now