Let's assume the following classes:
public class Author
{
public virtual string Name { get; set; }
public virtual List<Book> Books { get; set; }
}
public class Book
{
public virtual string Name { get; set; }
public virtual Author Author { get; set; }
}
public class Controller
{
public void DeleteBook(Book book)
{
var author = book.Author; //first check if it is loaded, not to invoke lazy loading?
author.Books.Remove(book) //check if the books collection is loaded?
book.Author = null;
Context.Set<Book>().Remove(book);
}
}
My question is - is there a way in EF to check the two 'is loaded' states? I want to ensure that the books author and the books collection are not loaded only to be disassociated.
I want to write something like:
public class Controller
{
public void DeleteBook(Book book)
{
if (EF.IsLoaded(book.Author)) //has it been (lazy) loaded / initialized?
{
if (EF.IsLoaded(book.Author.Books) //has it been (lazy) loaded / initialized?
{
book.Author.Books.Remove(book);
}
book.Author = null;
}
Context.Set<Book>().Remove(book);
}
}
Is this possible?