1

I have a Job entity with an optional Contract child entity (using Entity Framework)

    public class Job
    {
        public string Title { get; set; }
        public Guid? ContractId { get; set; }
        public virtual Contract Contract { get; set; }
    }

    public class Contract
    {
        public DateTime StartDate{ get; set; }
    }

I have the following DTO

    public class JobDto
    {
        public string Title { get; set; }
        public DateTime ContractStartDate { get; set; }
    }

I project to the DTO using AutoMapper Queryable extensions.

c.CreateMap<WorkTask, WorkTaskDtoLite>();

var jobDtos = _context.Jobs.ProjectTo<JobDto>().ToList();

Then this generates INNER joins, so does not return any JobDtos where there is no Contract. I need OUTER joins...

I'm really not sure how to fix this. I have referred to https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions but this does not seem to address this basic issue.

Any help appreciated!

UPDATE

Thanks all but seems to be an underlying issue with Entity Framework Core RC1.

See bug report here:

https://github.com/aspnet/EntityFramework/issues/3186

With suggested work-around here:

https://github.com/aspnet/EntityFramework/commit/c4e44471b6f41f96d37b1acfbbb4cc97cd11ee89

Mark Chidlow
  • 1,432
  • 2
  • 24
  • 43
  • 1
    For debugging, have you tried it without automapper? var jobDtos = _context.Jobs.Select(j => new JobDto { Title = j.Title, ContractStartDate = j.Contract.StartDate }).ToList(); – Steve Greene Apr 19 '16 at 13:30
  • Yes it is still an INNER join... – Mark Chidlow Apr 19 '16 at 15:45
  • 1
    It should do a left join when the FK is nullable. I don't work with GUIDs, but if you check the DB is it defined as nullable? http://stackoverflow.com/questions/26000345/allow-null-foreign-key-guid – Steve Greene Apr 19 '16 at 16:38
  • Yes nullable guid. EF7 so could be a bug? – Mark Chidlow Apr 19 '16 at 17:36
  • Haven't used 7 yet. You could maybe play with fluent config (HasDatabaseGeneratedOption, etc). – Steve Greene Apr 19 '16 at 17:42
  • Have you tried setting `DateTime` to `DateTime?`. I don't see what you expect it to do when the DateTime is null (i.e. Contract is null). Projection is far more limited, so it might not cope with that by default, in which case what about `NullSubstitute`? – Thomas Boby Apr 20 '16 at 12:14

0 Answers0