locked
Related entities has not been saved RRS feed

  • Question

  • Hi,

    I'm using code first approach againts existing db.

    Here is a portion of my code.

    public class AuthorDbContext : DbContext
     {
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
     modelBuilder.Entity<Author>().ToTable("Authors");
     modelBuilder.Entity<Book>().ToTable("Books"); 
     modelBuilder.Entity<Author>().HasMany(a => a.Books);
     base.OnModelCreating(modelBuilder);
     }
    
     public DbSet<Author> Authors { get; set; }
     public DbSet<Book> Books { get; set; }
     }
    
     public class Author
     {
     public Guid Id { get; set; }
     ...
     public virtual ICollection<Book> Books{ get; set; }
     }
    
     public class Book
     {
     public Guid Id { get; set; }
     ...
     public virtual Author Authors{ get; set; }
     }
    
    I create a new Author object, add related Book object to it
    
    Add Author object to entityContext, and then SaveChanges()
    
    The problem is only Author object has been saved, not the Book object
    
    Can you show me what I'm missing?
    

    Thank you



    Wednesday, May 25, 2011 4:04 AM

Answers

  • Hi,

    Did you add the new Book to the Books collection on the Author? If not then you will need to add the Book to the context as well.

    ~Rowan

    • Proposed as answer by Rowan Miller Wednesday, May 25, 2011 7:57 PM
    • Marked as answer by Jackie-Sun Wednesday, June 1, 2011 8:03 AM
    Wednesday, May 25, 2011 7:57 PM

All replies

  • I created a clean project and run the above code. I don't have your database so CF created one. And everything worked fine, I could add a Book and an Author.

    My test code was something like:

             Book b = new Book();
    
             Author a = new Author { Books = new HashSet<Book> { b } };
    
             using (var ctx = new AuthorDbContext())
    
             {
    
                ctx.Authors.Add(a);
    
                ctx.SaveChanges();
    
             }

    Maybe it's an issue with your existing database?  You could try comparing your database and the one generated by CF with this mapping.  

    You could also try generating a mapping from your database using EF Power Tools:  http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

    The mapping generated by the tool is very verbose (everything is mapped explicitly without relying on conventions), but it might give you a hint about what is wrong.


    Wednesday, May 25, 2011 7:15 AM
  • I installed the add-in to generate the codefirst model and use it, but it does not work. Only the parent is saved, not the related children

    Anyone helps ?

    Wednesday, May 25, 2011 8:26 AM
  • Hi,

    Did you add the new Book to the Books collection on the Author? If not then you will need to add the Book to the context as well.

    ~Rowan

    • Proposed as answer by Rowan Miller Wednesday, May 25, 2011 7:57 PM
    • Marked as answer by Jackie-Sun Wednesday, June 1, 2011 8:03 AM
    Wednesday, May 25, 2011 7:57 PM