Assuming your navigation property:
public virtual Entry WinningEntryId { get; set; }
actually called WinningEntry :
public virtual Entry WinningEntry { get; set; }
in fact, it looks like you are modeling a one-to-many relationship, not a one-to-one relationship. An event can contain many records, and a record can belong to one event? It is just that one of these entries will be marked as a winning entry.
I would prefer not to use data annotations and stick to a free API for: a) managing your entire configuration in a central place and b) not confusing persistence problems with domain objects.
In this case, you can customize your mappings using the free API as such:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Event>() .HasOptional(ev => ev.WinningEntry) .WithMany() .HasForeignKey(ev => ev.WinningEntryId) .WillCascadeOnDelete(false); modelBuilder.Entity<Entry>() .HasRequired(en => en.Event) .WithMany(ev => ev.Entries) .HasForeignKey(en => en.EventId); }
This will simulate a relationship with:
Events EventId (PK) EventName WinningEventId (FK, null) Entries EntryId (PK) EventId (FK, not null)
source share