I am new to [tag:Entity Framework] and having difficulties finding information on how to map [tag:navigation properties] in my Create Dto.
I have a JobCreateDto that needs to accept an AddressId and map from the Address database.
I have the following [tag:Fluent API] code but when I send a post request it asks for the other required properties for the address instead of just looking up the existing address by ID. It appears to be trying to create a new address.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder
.Entity<Job>()
.HasOne(p => p.Address);
}
Here is my HttpPost controller method:
[HttpPost]
public ActionResult<JobReadDto> CreateJob(JobCreateDto JobCreateDto)
{
var JobModel = _mapper.Map<Job>(JobCreateDto);
_repository.CreateJob(JobModel);
_repository.SaveChanges();
var JobReadDto = _mapper.Map<JobReadDto>(JobModel);
return CreatedAtRoute("GetJobById", new {Id = JobReadDto.Id}, JobReadDto);
}
The Create Dto is quite long but here is the navigation property
public virtual Address Address { get; set; }
Any help is appreciated. I remember doing this in a tutorial on Youtube but it was 8 hours into it and I'm not able to find it or a similar tutorial.
Thanks RyanS
I tried setting up the relationship using Fluent API in the OnModelCreating method of the DbContext but it appears to be trying to create a new address.
Edit to include Dto and Job class
JobCreateDto
using System.ComponentModel.DataAnnotations;
using TreffLandscapingDotNetApp.Models;
namespace TreffLandscapingDotNetApp.Dtos
{
public class JobCreateDto
{
public DateTime DateOfWork { get; set; }
public long Measure { get; set; }
public DateTime StartTime { get; set; }
public string StartLoc { get; set; }
public DateTime EndTime { get; set; }
public string EndLoc { get; set; }
public double startLatitude { get; set; }
public double startLongitude { get; set; }
public double endLatitude { get; set; }
public double endLongitude { get; set; }
public Boolean wasSaltUsed { get; set; }
public Boolean wasBinRefilled { get; set; }
public double TotalTime { get; set; }
public double Subtotal { get; set; }
public double Tax { get; set; }
public double Total { get; set; }
public JobStatus Status { get; set; }
public virtual Address Address { get; set; }
public Agreement Agreement { get; set; }
public int ServiceID { get; set; }
public int ResourceID { get; set; }
}
}
Job class
using System.ComponentModel.DataAnnotations;
namespace TreffLandscapingDotNetApp.Models
{
public struct Coords
{
public double x, y;
public Coords(double p1, double p2)
{
x = p1;
y = p2;
}
}
public enum JobStatus
{
New,
Scheduled,
Closed,
Completed
}
public class Job
{
[Key]
[Required]
public int Id { get; set; }
public DateTime DateOfWork { get; set; }
public long Measure { get; set; }
public DateTime StartTime { get; set; }
public string StartLoc { get; set; }
public double startLatitude { get; set; }
public double startLongitude { get; set; }
public DateTime EndTime { get; set; }
public string EndLoc { get; set; }
public double endLatitude { get; set; }
public double endLongitude { get; set; }
public Boolean wasSaltUsed { get; set; }
public Boolean wasBinRefilled { get; set; }
public double TotalTime { get; set; }
public double Subtotal { get; set; }
public double Tax { get; set; }
public double Total { get; set; }
public JobStatus Status { get; set; }
public virtual Address Address { get; set; }
public Agreement Agreement { get; set; }
public int ServiceID { get; set; }
public int ResourceID { get; set; }
}
}