I have an existing database which I would like to use Entity Framework Code First against in the most simple way possible. It is only a small database.
I have created simple POCO classes which mirror the database tables:
e.g.
public class Author
{
[Key]
public int AuthorID { get; set; }
public string AuthorName { get; set; }
public virtual ICollection<Books> Books { get; set; }
}
public class Books
{
[Key]
public int BookID { get; set; }
public string BookName { get; set; }
public int AuthorID { get; set; }
}
And a DbContext as follows:
public class Entities : DbContext
{
public Entities(string connString)
: base(connString)
{
}
public DbSet<Author> Authors { get; set; }
public DbSet<Books> Books { get; set; }
When I run my application, and select the first Author from my database, the AuthorID and AuthorName properties are populated correctly. However, the collection of Books is not populated. Instead there is an exception of type 'System.Data.EntityCommandExecutionException', and an inner exception of: 'Invalid column name 'Author_AuthorID'.
How can I establish correctly the link between Author and Books? (i.e. one to many, one Author can have many Books). I have created the Code First very simply - no migrations or auto-generation in any way, and would like to keep it as simple as this.
Many thanks for any help, Martin
