views:

60

answers:

2

EDIT: this happen only on larger scale projects with repositories. Is there anybody using EF4 with code first approach and using repositories? please advice me

Hi. Im currently working with EF4 Code First Classes. In my test project I got two classes, Author and Book (author got books). What I'm trying to do is that I HAve a AddBook in my Author class, but that wont seem to work like I can't Add it to the collection.. here are my classes and two different exceptions.

 public class Book
{
    public virtual int BookId { get; set; }
    public virtual string Title { get; set; }
    public virtual Author Author { get; set; }
}

public class Author
{
    public virtual int AuthorId { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }

    public Author()
    {
        Books = new Collection<Book>();
    }


    public void AddBook(Book book)
    {
        book.Author = this;
        Books.Add(book);
    }
}

exception: The property 'Books' on type 'Author_4CF5D4EE954712D3502C5DCDDAA549C8E5BF02A0B2133E8826A1AC5A40A15D2A' cannot be set because the collection is already set to an EntityCollection.

I change the Author class to this

public class Author
{
    public virtual int AuthorId { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }


    public void AddBook(Book book)
    {
        book.Author = this;
        Books.Add(book);
    }
}

Exception: Object reference not set to an instance of an object.

cannot be set because the collection is already set to an EntityCollection.

And thats only natural that I get that exception because the Collection is not set to a new, but then I get that first exception. so how is this done with code first in EF?

maybe I should add that my It might collide with my DbSet?

public class EntityContext : DbContext, IUnitOfWork
{
    public DbSet<Author> Authors { get; set; }
    public DbSet<Book> Books { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.IncludeMetadataInDatabase = false;
    }

    public void Save()
    {
        SaveChanges();
    }
}
+1  A: 

I've got code first working, but the only (major) difference with my code is I initialize it as a list rather than a collection... so my code reads something like:

public class Author
{
    public virtual int AuthorId { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }

    public Author()
    {
        Books = new List<Book>();
    }
}

Also, I just add directly to the Books collection - there is no need to add the book to the collection AND add the author to the book because the entity framework should take care of that.

If this doesn't work for you. Let me know.

HTHs,
Charles

Charlino
This works fine when I try on a small test application but when I implement this type on my larger scale project with repositories and services the problem still remains..
Dejan.S
I'm sorry, what is it that *works fine* on a small test application? My project that I'm working on is a large(ish) application with repositories and services - I don't see how this should make a difference though... it should scale without any issues.
Charlino
Me neither but it does or did.. I removed the Virtual on my properties and kept it on the ICollection and it works on my full scale project like it suppose to now, kinda like a DOH moment but I wont say to much yet, so far so good need to test this more later today.. thanks for your reply Charlino
Dejan.S
A: 

Virutal on all the properties was the issue here for me.. Don't get why it have a impact like that when I did scaled it up to my project but that's how it was.. Hope this helps anybody that got the same issue..

Dejan.S