Hi,
I have an entity called Job. It has property (child object) Report (many to one). Each Job has only one report.
While saving the job through a standard ZERO setup (application service with all the standard setup), it's not possible to only save the Id of the report entity while saving only a job. At current, at the moment of update, Job has a populated property Report indeed. When updating it is going through the automated validation of DBContext etc. etc. When there are child properties within the report object (which aren't loaded ofcourse) then the save of the Job entity will fail because of the Report entity! But I don't want to save Report! I want to save Job with only the Report ID (the reference)..
How can I force to only store Job with report Id/reference and not going to store Report entity as well?!
This is partly a EF question but since the DBContext is hidden in the standard JobRepository it's caused by zero setup.
<code> public async Task CreateOrUpdateJob(CreateOrUpdateJobInput input) {
if (input.Job.Id > 0)
{
//existing Job
var Job = await _JobRepository.GetAsync(input.Job.Id);
input.Job.MapTo(Job);
await _JobRepository.UpdateAsync(Job);
}
else
{
//new Job
await CreateJobAsync(input);
}
}
</code>
5 Answer(s)
-
0
Hi,
Can you share your CreateOrUpdateJobInput class ?
-
0
Hi,
public class CreateOrUpdateJobInput : IInputDto { [Required] public JobEditDto Job { get; set; } }
[AutoMapFrom(typeof(Job))] [AutoMapTo(typeof(Job))] public class JobEditDto : FullAuditedEntityDto { public Report Report { get; set; } public string CronSchedule { get; set; } public ReportOutputType OutputType { get; set; } public DateTime? LastRun { get; set; } public bool Active { get; set; } public string Destination { get; set; } }
-
0
Hi,
Thanks for thinking with me :-) I'm currently testing a procedure with in I added ReportId property to Entity Job (definition in .core) and change the dto property report into reportId.
I'll update in a sec.
-
0
Hi,
No problem at all :)
I believe the problem is related to Report entity in JobEditDto. You shouldn't use entities in dtos. You can take a look at this document for detailed explanation <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Data-Transfer-Objects">http://aspnetboilerplate.com/Pages/Docu ... er-Objects</a>.
Instead of Report entity, you should create a dto class and use it in your JobEditDto.
By the way, you can also use
[AutoMap(typeof(Job))]
instead of these two lines
[AutoMapFrom(typeof(Job))] [AutoMapTo(typeof(Job))]
It does the two way mapping.
-
0
Hi,
Way to go! Yes, we should not use entity objects in dto's and yes, you can add the Id of an entity in the definition of an entity like copied below. Then you can set the proper id yourself without the use of instances of the object, in this case report.
Furthermore, set the property Report to virtual when you want it to be lazy loaded!
public class Job : EntityBase { public int ReportId { get; set; } public virtual Report Report { get; set; } public string CronSchedule { get; set; } public ReportOutputType OutputType { get; set; } public DateTime LastRun { get; set; } public bool Active { get; set; } public string Destination { get; set; } }