0

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.

enter image description here

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

3 Answers 3

1

Fluent API Approach :

modelBuilder.Entity<Author>().HasMany(a => a.Books).WithRequired().HasForeignKey(b => b.AuthorID);

update : with this fluent API you don't have to add property Author in Class Books

if you want to set Books to not required, you must set property AuthorID to int?

    modelBuilder.Entity<Author>().HasMany(a => a.Books).HasForeignKey(b => b.AuthorID);

Books class :

public class Books
{
    [Key]
    public int BookID { get; set; }
    public string BookName { get; set; }
    public int? AuthorID { get; set; }

}
Sign up to request clarification or add additional context in comments.

Comments

1

Add Author property (without it single AuthorID property not matter at context of relation with Authors table.):

public class Books
{
    [Key]
    public int BookID { get; set; }
    public string BookName { get; set; }
    public int AuthorID { get; set; }

    //add this(attribute is not needed if you use EF6 or higher):
    [ForeignKey("AuthorID")]
    public virtual Author Author  { get; set; }
}

5 Comments

Thanks Slava. I found this still gives an error, but adding the annotation [ForeignKey("Author")] in addition fixes this (see below).
i think you should add annotation [ForeignKey("Author")] in AuthorID property. to prevent redundant
It is strange, at my case all working correct and attribute not needed due to names conventions: AuthorID = Author + ID. May be it depends on version of EF, in case of EF6 it is works, what is your version?
I am using version 4
That is anwer - for EF6 you can skip this attribute.
0

Well, this is embarassing, I found an answer minutes after my post. I will post the answer, rather than deleting my question in case it helps anyone else:

public class Books
{
    [Key]
    public int BookID { get; set; }
    public string BookName { get; set; }

    [ForeignKey("Author")]
    public int AuthorID { get; set; }     
    public Author Author { get; set; }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.