8528

Question:
Is there a way to create a zero-or-one to one-or-many relationship in Entity Framework? I've seen plenty examples showing a 0..1 to 0..* relationship, but I want to be sure that in the given example Foo can only exist if it has at least one Bar.
class Foo
{
List<Bar> Bars { get; set; } // Must at least have one Bar
}
class Bar
{
public Foo Foo { get; set; } // Foo is nullable
}
I see that this is not easily achieved by SQL since I want a kind of NOT NULL
at the Foo table instead of at the Bar table, but can Entity Framework handle this?
Correct me if i'm wrong, but I think you want something like this:
modelBuilder.Entity<Foo>()
.HasMany(t => t.Bars)
.WithOptionalPrincipal(t => t.Foo);