0

I have an Orders wpf grid, this view includes 2 foreign keys, Customers and Vendors. The Create Orders View includes these 2 foreign keys too, as well as imports extra info like vendor id and name and phone number.

It is not always necessary to fill out the vendor on this view, however everything I've tried doesn't allow the vendor to be null.

I've tried stuff like this but the flow here doesn't match my case, and can't seem to access the right properties when trying to manipulate the flow. https://stackoverflow.com/a/49652484/4583310

putting "?" after my variables is giving foreign key errors as well.

how can I get this baby to accept null values?

 public readonly struct CreateOrderRequest
{
    //[Required]
    public int VendorId { get; init; }
    [Required]
    public int CustomerId { get; init; }
    public string SearchUrl { get; init; }
}


 public class Order
{
    [Key]
    public int Id { get; set; }
    public string Description { get; set; }            

    [ForeignKey(nameof(Vendor))]
    public int VendorId { get; set; }
    public Vendor Vendor { get; set; }
    public string SearchUrl { get; set; }

    [ForeignKey(nameof(Customer))]
    public int CustomerId { get; set; }
    public Customer Customer { get; set; }
}

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
       // modelBuilder.Entity<Order>().HasMany(a => Vendor);
        OnModelCreatingPartial(modelBuilder);
    }

I get errors on the await command

  public async Task<Order> CreateOrderAsync(Order order)
    {
        var entry = await _ordersDbSet.AddAsync(order);
        await _dbContext.SaveChangesAsync();
        return entry.Entity;
    }

which stem from trying to post from the orders controller. I turned off validating the vendor

 public async Task<IActionResult> CreateOrderAsync(CreateOrderRequest request)
    {
        if (await ValidateCreateOrderRequestAsync(request) is false)
            return BadRequest();

        var order = _mapper.Map<Order>(request);
        order = await _ordersUnitOfWork.CreateOrderAsync(order);
        order = await _ordersUnitOfWork.GetOrderAsync(order.Id);

        var orderDto = _mapper.Map<OrderDto>(order);
        return Ok(orderDto);
    }
drpepper1324
  • 113
  • 9

2 Answers2

1

nullable foreign key is not a good idea at all but some times we have to use it. you can use it by changing

public int VendorId { get; set; }

to

public int? VendorId { get; set; }

in Order class

  • For whatever reason, my OrderController and my Order Model don't agree with using the ? marks. I"ll add that code as well, I just tracked it down using the event viewer, but that left me with a more questions than it answered. – drpepper1324 Feb 01 '22 at 09:37
0

Took me a while, but I finally figured out I need to place some of those ?'s in my requests and my models where my foreign key id's were laying, not just for the Model that is currently in view. Edit: after fixing it this way, I had to go in and change some validation checks and Also add the ? to some of my DTO's

drpepper1324
  • 113
  • 9