I am new to Entity Framework Core 3.1 and ASP.NET Core 3.1 in general. What I am trying to do is set up my tables Categories
, Manufacturers
, Conditions
, Locations
and AssetFiles
to have a one-to-many relationship with my Asset
table.
These are the data models that I have currently
The Asset
table
public class Asset
{
public int Id { get; set; }
public Categories Category { get; set; }
public Manufacturers Manufacturer { get; set; }
public string Model { get; set; }
public string SerialNumber { get; set; }
public string PurchasePlace { get; set; }
public int Quantity { get; set; }
public DateTime AcquiredDate { get; set; }
public string PurchasePrice { get; set; }
public string CurrentValue { get; set; }
public Conditions Condition { get; set; }
public Locations Location { get; set; }
public AssetFiles Files { get; set; }
}
Categories
table
public class Categories
{
public int Id { get; set; }
public string CategoryName { get; set; }
public ICollection<Asset> Assets { get; set; }
}
The other tables have pretty much the same format. I can create the migration and update the database so that it is created. It looks to me like it is working the way that I want it to.
My question is how do I access these tables in my Assets controller in the [HttpPost]
method to be able to add a category, manufacturer, condition, Location, to the Asset
when it is posted cause right now With the controller scaffolded all I'm getting is null for those sections of my asset
Here is my post method of my assetController
:
[HttpPost]
public async Task<ActionResult<Asset>> PostAsset([FromForm] Asset asset)
{
_context.Assets.Add(asset);
await _context.SaveChangesAsync();
return CreatedAtAction("GetAsset", new { id = asset.Id }, asset);
}
I'm just looking for a little direction on how I need to look at this to move forward.
Any help would be appreciated.
Edit: 2
Also just to mention I am using a VueJs Front End not sure if that matters to the tips you are giving or if accessing the data is the same .