Home > database >  .Net Core 3.1 Entity Framework: Inserting Simultaneously in One-To-Many Relationship
.Net Core 3.1 Entity Framework: Inserting Simultaneously in One-To-Many Relationship

Time:10-08

Introduction:

I am building a Web Application using .Net Core 3.1 and have two models: 1- InvoiceHeader, 2- InvoiceLine.

Where an InvoiceHeader will contain multiple InvoiceLine (i.e. One-To-Many Relationship).

Problem I am trying to create the InvoiceHeader and add multiple InvoiceLine(s) to it within the same view. When I Launch the app with the below code I get this error: ArgumentNullException: Value cannot be null. (Parameter 'entity') when I try to add data to my database at run time, referencing _context.InvoiceLine.Add(InvoiceLine); from the Create.cshtml.cs file.

Goal: To create the InvoiceHeader and InvoiceLine simultaneously within the same Page/Form. Specifically, I want to know how this is handled through the PageModel code (create.cshtml.cs).

My Code:

InvoiceHeader.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace PRArchivalSystem.Model
{
    public class InvoiceHeader
    {
        public int InvoiceHeaderId { get; set; }
        [Required]
        public string InvoiceNumber { get; set; }
        [Required]
        public DateTime InvoiceDate { get; set; }
        [Required]
        public string InvoiceVendor{get; set;}
        [Required]
        public double InvoiceTotal { get; set; }
        public ICollection<InvoiceLine> InvoiceLines { get; set; }
    }
}

InvoiceLine.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

using System.Linq;
using System.Threading.Tasks;

namespace PRArchivalSystem.Model
{
    public class InvoiceLine
    {
        public int InvoiceLineId { get; set; }
        public string LineDescription { get; set; }
        public double LineAmount { get; set; }
        public double LineQuantity { get; set; }
        public bool LineIsTaxed { get; set; }
        public double LineTax { get; set; }
        public double LineTotal { get; set; }
        public int InvoiceHeaderId { get; set; }
        public InvoiceHeader InvoiceHeader { get; set; }
    }
}

ApplicationDbContext.cs

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PRArchivalSystem.Model
{
    public class ApplicationDbContext : IdentityDbContext 
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        public DbSet<Order> Order { get; set; }
        public DbSet<Vendor> Vendor { get; set; }
        public DbSet<Department> Department { get; set; }
        public DbSet<InvoiceHeader> InvoiceHeader { get; set; }
        public DbSet<InvoiceLine> InvoiceLine { get; set; }
    }
}

The Model Page: Create.cshtml

@page
@model PRArchivalSystem.Pages.InvH.CreateModel

<h4>InvoiceHeader</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="InvoiceHeader.InvoiceNumber" class="control-label"></label>
                <input asp-for="InvoiceHeader.InvoiceNumber" class="form-control" />
                <span asp-validation-for="InvoiceHeader.InvoiceNumber" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="InvoiceHeader.InvoiceDate" class="control-label"></label>
                <input asp-for="InvoiceHeader.InvoiceDate" class="form-control" />
                <span asp-validation-for="InvoiceHeader.InvoiceDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="InvoiceHeader.InvoiceVendor" class="control-label"></label>
                <input asp-for="InvoiceHeader.InvoiceVendor" class="form-control" />
                <span asp-validation-for="InvoiceHeader.InvoiceVendor" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="InvoiceHeader.InvoiceTotal" class="control-label"></label>
                <input asp-for="InvoiceHeader.InvoiceTotal" class="form-control" />
                <span asp-validation-for="InvoiceHeader.InvoiceTotal" class="text-danger"></span>
            </div>
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="InvoiceLine.LineDescription" class="control-label"></label>
                <input asp-for="InvoiceLine.LineDescription" class="form-control" />
                <span asp-validation-for="InvoiceLine.LineDescription" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="InvoiceLine.LineAmount" class="control-label"></label>
                <input asp-for="InvoiceLine.LineAmount" class="form-control" />
                <span asp-validation-for="InvoiceLine.LineAmount" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="InvoiceLine.LineQuantity" class="control-label"></label>
                <input asp-for="InvoiceLine.LineQuantity" class="form-control" />
                <span asp-validation-for="InvoiceLine.LineQuantity" class="text-danger"></span>
            </div>
            <div class="form-group form-check">
                <label class="form-check-label">
                    <input class="form-check-input" asp-for="InvoiceLine.LineIsTaxed" /> @Html.DisplayNameFor(model => model.InvoiceLine.LineIsTaxed)
                </label>
            </div>
            <div class="form-group">
                <label asp-for="InvoiceLine.LineTax" class="control-label"></label>
                <input asp-for="InvoiceLine.LineTax" class="form-control" />
                <span asp-validation-for="InvoiceLine.LineTax" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="InvoiceLine.LineTotal" class="control-label"></label>
                <input asp-for="InvoiceLine.LineTotal" class="form-control" />
                <span asp-validation-for="InvoiceLine.LineTotal" class="text-danger"></span>
            </div>

            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

The Model Controller:Create.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using PRArchivalSystem.Model;

namespace PRArchivalSystem.Pages.InvH
{
    public class CreateModel : PageModel
    {
        private readonly PRArchivalSystem.Model.ApplicationDbContext _context;

        public CreateModel(PRArchivalSystem.Model.ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult OnGet()
        {
            return Page();
        }

        [BindProperty]
        public InvoiceHeader InvoiceHeader { get; set; }
        public InvoiceLine InvoiceLine { get; set; }


        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            
            _context.InvoiceHeader.Add(InvoiceHeader);
            _context.InvoiceLine.Add(InvoiceLine);
            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }
    }
}

Any help will be appreciated as I am new to both C# and .Net EF.

Thanks

CodePudding user response:

When I Launch the app with the below code I get this error: ArgumentNullException: Value cannot be null. (Parameter 'entity')

This error is caused by null object value of InvoiceLine. You need add [BindProperty].

Besides, you also need modify your backend code below to make insert operation successfully:

[BindProperty]
public InvoiceHeader InvoiceHeader { get; set; }
[BindProperty]
public InvoiceLine InvoiceLine { get; set; }

public async Task<IActionResult> OnPostAsync()
{
    if (!ModelState.IsValid)
    {
        return Page();
    }
    
    _context.InvoiceHeader.Add(InvoiceHeader);
    //_context.InvoiceLine.Add(InvoiceLine);
    InvoiceHeader.InvoiceLines = new List<InvoiceLine>();
    InvoiceHeader.InvoiceLines.Add(InvoiceLine);
    await _context.SaveChangesAsync();

    return RedirectToPage("./Index");
}
  • Related