0

Somebody edit the join table, because I can't remember the word for a table that contains only 2 foreign keys for 2 entities. Edit: I think it's called a link table?

The db-model looks like this DB-Model entities. And in the code for the models and DTO's, datadock has a List<Standard>. So the code looks like this:

    private List<DatadockDTO> GetDatadocksByDataportId(Guid id)
    {
        var query = from r in _ddRepository.GetIQueryable()
                    where r.DataportId == id
                    select new DatadockDTO
                    {
                        Id = r.Id,
                        Name = r.Name,                            
                        Standards = r.Standards //Error not possible, the only suggestion it gives is DataDockStandards
                        //I used to put GetStandardsByDatadockId(r.Id) here but there is another way apparently
                    };
        List<DatadockDTO> datadocks = query.ToList();
        return datadocks;
    }

But somebody I work with told me Entity Framework allows you to do something like that. But I'm not sure where to look for.

The extra code I made how I thought it was supposed to work:

    private List<StandardDTO> GetStandardsByDatadockId(int id)
    {
        var query = from r in _standardRepository.GetIQueryable()
                    where r.DataDockStandards.Any(dds => dds.DatadockId == id)
                    select new StandardDTO
                    {
                        Id = r.Id,
                        Name = r.Name,
                        StandardVersions = GetStandardVersionsByStandardId(r.Id)
                    };
        List<StandardDTO> standards = query.ToList();
        return standards;
    }

    private List<StandardVersionDTO> GetStandardVersionsByStandardId(int id)
    {
        var query =
          from r in _svRepository.GetIQueryable()
          where r.StandardId == id
          select new StandardVersionDTO
          {
              Id = r.Id,
              Name = r.Name,
          };
        List<StandardVersionDTO> versions = query.ToList();
        return versions;
    }

EDIT (next day): okay so apparently somehow I can select r.Standards somehow. I didn't change anything, I could suddenly just choose r.Standards without getting an error...

  • Your code is a black box. We don't know the result of these `GetIQueryable` calls. Also, it's not clear what you're actually asking. You show code, but what's wrong with it? – Gert Arnold Mar 16 '21 at 16:07

1 Answers1

2

You need to configure many-to-many relationship.

Here is how it's done in EF Core (keep in mind, it's only for version 5 and above, it is not supported even in 3.1).

And here is how it's done in EF 6.

If you do it correctly, your DataDock entity will have Standards navigation property and you'll be able to use it in LINQ queries.

Agrgg
  • 279
  • 2
  • 5