Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Framework entity , +
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:
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 19-May-13 20:31pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 20 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100