EntityFramework6.Npgsql icon indicating copy to clipboard operation
EntityFramework6.Npgsql copied to clipboard

PostgresException: 42P01: relation "public.Authors" does not exist

Open waqasm78 opened this issue 6 years ago • 2 comments

Hi, I am using PostgreSQL database with EF6 code first approach, but I am getting the following exception

PostgresException: 42P01: relation "public.Authors" does not exist

Here is the model and context class.

public class BookStore : DbContext
    {
        public BookStore() : base(nameOrConnectionString: "Default")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema("public");
        }

        public DbSet<Author> Authors { get; set; }
        public DbSet<Book> Books { get; set; }
    }

    public class Author
    {
        public int AuthorId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }
        public List<Book> Books { get; set; }
    }

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

when inseting same data I am getting the exception.

using (var context = new BookStore())
{
    var authors = new List<Author>
    {
        new Author
        {
            FirstName ="Carson",
            LastName ="Alexander",
            BirthDate = DateTime.Parse("1985-09-01"),
            Books = new List<Book>()
            {
                new Book { Title = "Introduction to Machine Learning"},
                new Book { Title = "Advanced Topics in Machine Learning"},
                new Book { Title = "Introduction to Computing"}
            }
        }
    };

    context.Authors.AddRange(authors);
    context.SaveChanges();
}

waqasm78 avatar Jun 30 '19 14:06 waqasm78

Have you solved this? I am facing the same issue right now.

taylangezici1 avatar Sep 23 '20 17:09 taylangezici1

Ok so this may be dumb but I was having this problem and it turned out that I had not updated the database since adding the migration dotnet ef database update

Dinglebarry9 avatar Dec 03 '20 02:12 Dinglebarry9