.NET 6 - EF : how do I insert one to many relation ship with null reference?

Viewed 72

I have a one-to-many relationship with Author and Books. What I want to do is insert authors, but I don't know what books they released yet, so the books are empty for now.

I've tried different logic and relationship connection between author and books but still get:

Object reference not set to an instance of an object.

My code:

public class Author
{
    public Guid Id { get; set; }
    public string name { get; set; }
    public ICollection<Book>? Book { get; set; } = new List<Book>();
}

public class Books
{
    public Guid Id { get; set; }
    public int Page { get; set; }
    public string? Name { get; set; }
    public Author? Author{ get; set; }
    public Guid AuthorId { get; set; }
}

Relationship connection between Author and Books

modelBuilder.Entity<Author>()
            .HasMany(u => u.Book)
            .WithOne(a => a.Author)
            .HasForeignKey(aa => aa.AuthorId)
            .OnDelete(DeleteBehavior.Cascade);

Attempt at inserting data into Author with no books yet:

_context.Author.Add(request.Author);
1 Answers

Check this out https://www.learnentityframeworkcore.com/configuration/one-to-many-relationship-configuration :

Whether starting with the Company or Employee entity, this configuration will result in an optional relationship. A foreign key shadow property named CompanyId will be introduced by EF Core to the Employee entity which will be nullable.

Btw you don't need following specify nullable for string and Book - they are nullable by default. Book is class which is reference type and string is "special" reference type which has some behaviour of value type.

UPDATE COMPLETE WORKING EXAMPLE

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace EfCore.SqlRepository
{
public class BooksContext : DbContext
{
    public DbSet<Author> Authors { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Author>()
        .HasMany(u => u.Book)
        .WithOne(a => a.Author)
        .HasForeignKey(aa => aa.BookId)
        .OnDelete(DeleteBehavior.Cascade);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseInMemoryDatabase("Books");


    public void AddAuthor()
    {
        var author = new Author
        {
            Id = Guid.NewGuid(),
            name = "Jojn Doe"
        };

        Authors.Add(author);
        base.SaveChanges();
        
    }

    public class Author
    {
        public Guid Id { get; set; }
        public string name { get; set; }
        public ICollection<Book> Book { get; set; } = new List<Book>();
    }

    public class Book
    {
        public Guid Id { get; set; }
        public int Page { get; set; }
        public string Name { get; set; }
        public Author Author { get; set; }
        public Guid BookId { get; set; }
    }
}

}

Working example

Related