To illustrate my problem, suppose I have a data model where I have a collection of Books, each of which has one or more Drafts, and also a “current” Draft. I’d like my model to look something like this:
class Book
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Draft> Drafts { get; set; }
public virtual Draft CurrentDraft { get; set; }
}
class Draft
{
public int Id { get; set; }
public virtual int BookId { get; set; }
public virtual Book Book { get; set; }
public string Description { get; set; }
}
I have a DbContext that looks like this:
class TestDbContext : DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Draft> Drafts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
base.OnModelCreating(modelBuilder);
}
}
…and a simple program like this:
static void Main(string[] args)
{
Database.SetInitializer<TestDbContext>(new DropCreateDatabaseAlways<TestDbContext>());
using (var db = new TestDbContext())
{
var book = new Book() { Title = "War and Peace", Drafts = new List<Draft>() };
var draft1 = new Draft() { Book = book, Description = "First Draft" };
book.Drafts.Add(draft1);
var draft2 = new Draft() { Book = book, Description = "Second Draft" };
book.Drafts.Add(draft2);
book.CurrentDraft = draft2;
db.Books.Add(book);
db.SaveChanges();
foreach (var b in db.Books)
{
Console.WriteLine("Book {0}: {1}", b.Id, b.Title);
foreach (var d in book.Drafts)
Console.WriteLine("\tDraft ID {0}: {1} from Book ID {2}", d.Id, d.Description, d.BookId);
Console.WriteLine("Current draft has ID {0}", b.CurrentDraft.Id);
}
Console.ReadKey();
}
}
When the DB is created by EF, it looks like this:

[I’m not wild about the extra Book_Id column, but I could live with it if it worked.]
Sadly, when I run the test program, it fails at the SaveChanges call, with an exception:
An error occurred while saving entities that do not expose foreign key
properties for their relationships. The EntityEntries property will
return null because a single entity cannot be identified as the source
of the exception. Handling of exceptions while saving can be made
easier by exposing foreign key properties in your entity types. See
the InnerException for details.
I’ve tried messing about with the fluent API in my OnModelCreating, but when I try to configure it that way it gets confused by the fact that there are two FK relationships between Books and Drafts. I’m not familiar enough with the fluent stuff (or EF generally) to know how to do it properly.
Is this even possible in EF code first?
Your inner exception is that EF can’t figure out the order to save things to the database to create something properly. If you call save changes in multiple places to force the order your test app will work:
Regarding the second book_id – you could use this fluent:
which produces this database: