8528

EF 0..1 to 1..* relationship

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?

Answer1:

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);

Recommend

  • How to determine the association between a VB6 app and an exe instanced with CreateObject()
  • How to specify a multi-column UNIQUE constraint in code-first Entity Framework fluent API
  • Detaching entity along with referenced entities
  • CoreData basics – to-many relationship array data
  • Is it possible to define rest argument in OCaml?
  • Doctrine/Symfony entity generator and generating entity from one table
  • How to retrieve information from antrun back to maven?
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Why cepheus don't send int without quotes to orion?
  • GAE: Way to get reference to an HttpSession from its ID?
  • Force show.bind execution
  • java inputstream
  • Xcode 4 NSLog Macro link in Xcode 3
  • Meteor helpers not available in Angular template
  • Align navbar back button on right side
  • Window Size for Mac application
  • Build own AppleScript numerical error handling
  • Numpy divide by zero. Why?
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Trying to get generic when generic is not available
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • embed rChart in Markdown
  • File not found error Google Drive API
  • Turn off referential integrity in Derby? is it possible?
  • Qt: Run a script BEFORE make
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Add sale price programmatically to product variations
  • unknown Exception android
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How do I use LINQ to get all the Items that have a particular SubItem?