Entity Framework Core many to many relation error: Object reference not set to an instance of an object

0

I am new to C# / EF Core and don't understand why I am getting a null reference exception:

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=WMS

StackTrace:
at WMS.Pages.Artikels.Artikel_Verkauf_erfassen.ArtikelVerkaufErfassen.d__33.MoveNext() in D:\Project Arbeit\Code\WMS\Pages\Artikels\Artikel Verkauf erfassen\ArtikelVerkaufErfassen.razor.cs:line 44

I have a many to many relation and trying to get articles of branch:

void Add()
{
    var availableQuantity = BranchItems.Find(bi => bi.ArticleId == articleSale.ArticleId)
    .BrancheArticles.Find(ba => ba.ArticleId == articleSale.ArticleId).Quantity;
}

Class structure looks like this:

public class ArticleModel
{
    [Key]
    public int ArticleId { get; set; }

    [Required]
    public string ItemNumber { get; set; }

    [Required]
    public string ItemName { get; set; }

    [Required]
    [MaxLength(40)]
    [MinLength(10)]
    public string ArticleDescription { get; set; }

    [Range(50, 10000)]
    [DataType(DataType.Currency)]
    [Column(TypeName = "decimal(18, 2)")]
    public decimal Preis { get; set; }

    [Range(1,100, ErrorMessage = "sollte von 1 bis 100 Stück sein")]
    public int Menge { get; set; }

    public List<BranchArticle> BrancheArticles { get; set; } = new List<BranchArticle>();
}

public class BranchArticle
{
    public int BranchId { get; set; }
   
    public int ArticleId{ get; set; }

    public BranchModel Branch { get; set; } = new BranchModel();

    public ArticleModel Article { get; set; } = new ArticleModel();

    public int Quantity { get; set; }
}

public class BranchModel
{
    [Key]
    public int Id { get; set; }
    public Guid BranchId { get; set; }
    public string BranchCode { get; set; }
    public string BranchName { get; set; }
    public string Street { get; set; }
    public string HouseNr { get; set; }
    public string ZipCode { get; set; }
    public string Location { get; set; }
    public string ContactPerson { get; set; }
    public string TelefoneNr { get; set; }
    public string Email { get; set; }

    public List<BranchArticle> BrancheArticles { get; set; } = new List<BranchArticle>();  
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<BranchArticle>()
        .HasKey(ab => new { ab.BranchId, ab.ArticleId });

    modelBuilder.Entity<BranchArticle>()
        .HasOne<ArticleModel>(ab => ab.Article)
        .WithMany(am => am.BrancheArticles)
        .HasForeignKey(ab => ab.ArticleId);

    modelBuilder.Entity<BranchArticle>()
        .HasOne<BranchModel>(ab => ab.Branch)
        .WithMany(bm => bm.BrancheArticles)
        .HasForeignKey(ab => ab.BranchId);

    modelBuilder.Entity<BranchArticle>(entity => { entity.ToTable(name: "BranchArticles"); });

    base.OnModelCreating(modelBuilder);
}
c#
entity-framework-core
asked on Stack Overflow Apr 15, 2021 by jamy77 • edited Apr 16, 2021 by marc_s

1 Answer

0

Presumably one of the Find functions is failing (returning null).

I suggest you break up your code like this.

var branchItem = BranchItems.Find(bi => bi.ArticleId == articleSale.ArticleId);
if(branchItem == null) {
    // throw an error or print an error message, etc...
} else {
    var article = branchItem.BrancheArticles.Find(ba => ba.ArticleId == articleSale.ArticleId);
    if (article == null) {
        // throw an error or print an error message, etc...
    } else { 
        var quantity = article.Quantity;
    }
}

You might also think about using Where() + SingleOrDefault() instead of Find().

answered on Stack Overflow Apr 16, 2021 by Nicholas Hunter • edited Apr 16, 2021 by Nicholas Hunter

User contributions licensed under CC BY-SA 3.0