Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
i have problem on adding new entities to my context in entity framework code first
problem is that when i want to add new author to book or book to author is says :
"Object reference not set to an instance of an object." because author of book or book of author is null... while i`v set that.
here is my code:
C#
class Program
    {
        static void Main(string[] args)
        {
            var db = new Source();
            Publisher publisher = db.Publisher.Single( p=> p.Title == "Press");
            Book book = new Book() { ISBN = "123424", Publisher = publisher, Title = "Intro to .NET" };
            book.Authors.Add(new Author() { FirstName = "Bill", LastName = "Gates"});

            db.Books.Add(book);
            db.SaveChanges();
        }
    }

    public class Source: DbContext
    {
        public Source(): base("Main")
        {

        }

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Publisher>().HasKey(p => p.ID);
            modelBuilder.Entity<Book>().Property(p => p.Title).HasMaxLength(200);
        }
    }

    public class Publisher
    {
        public long ID { get; set; }

        public string Title { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
    }

    public class Book
    {
        public long ID { get; set; }

        public string Title { get; set; }
        public string ISBN { get; set; }

        public List<Author> Authors { get; set; }
        public Publisher Publisher { get; set; }
    }

    public class Author
    {
        public long ID { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

        public List<Book> Books { get; set; }
    }
Posted

1 solution

Hello RedSakura,

I have corrected your code here. you have missed creating new object of Books in Author class.
Note: I have checked with static data only not with Database. and this is complete working code.

C#
var db = new Source() { Authors = new List<Author>(), Publisher = new List<Publisher>(), Books = new List<Book>() };
            Publisher publisher = new Publisher() { Title = "Press", Address = "India", Email = "this@gmail.com", ID = 1 };
            db.Publisher.Add(publisher);
            publisher = db.Publisher.Single(p => p.Title == "Press");

            Book book = new Book() { ISBN = "123424", Publisher = publisher, Title = "Intro to .NET", Authors = new List<Author>() };
            book.Authors.Add(new Author() { FirstName = "Bill", LastName = "Gates" });
            db.Books.Add(book);


Thanks,
Imdadhusen
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900