henkmollema/Dommel

Question regarding multi mapping and foreign keys

raaffaaeell opened this issue · 5 comments

Hello there, currently I'm trying to add this extension (together with fluentmap)

I have a class that has 3 object properties

 public class Exame
      {
        public int Id { get; set; }

        [ForeignKey("ID_EXAME_MOTIVO")]

        public int ExameMotivoId { get; set; }

        public ExameMotivo ExameMotivo { get; set; }

        [ForeignKey("ID_EXAME_STATUS")]

        public int ExameStatusId { get; set; }

        public ExameStatus ExameStatus { get; set; }

        [ForeignKey("ID_EXAME_TIPO")]

        public int ExameTipoId { get; set; }

        public ExameTipo ExameTipo { get; set; }

  } 

The field names, as you can see, are not the same as the database ones, I tried using the foreignkey attribute like that, but I hit my first issue. When I use it like this, it first join Exame with ExameMotivo then it tries to join ExameMotivo with ExameStatus. What am I doing wrong there ?

Also, if I specify only one including object, in a query like this

conexao.GetAll<Exame, ExameStatus, Exame>((e, es) => { e.ExameStatus = es; return e; });

It supposedly works, but then I hit another problem. Using regular dapper, when I do this, I write my query and I can specify the splitOn parameter, with the GetAll method it is not possible. Should I just drop this extension and write the queries by hand again ?

If it helps, I have my ExameMap like this

 public class ExameMap : DommelEntityMap<Exame>

    {

        public ExameMap()

        {

            ToTable("EXAME", "PNCEBT");

            Map(c => c.Id).ToColumn("ID_EXAME").IsKey();

            Map(c => c.ExameMotivoId).ToColumn("ID_EXAME_MOTIVO");

            Map(c => c.ExameStatusId).ToColumn("ID_EXAME_STATUS");

            Map(c => c.ExameTipoId).ToColumn("ID_EXAME_TIPO");


            Map(c => c.ExameStatus).Ignore();

            Map(c => c.ExameMotivo).Ignore();

            Map(c => c.ExameTipo).Ignore();

        }

} 

After further investigation, I see that most of the problems I have are "resolved" in version 2 of this library (beta 7) together with a patch from the FluenMap.Dommel.

One major issue now, after fixing these I posted above (apparently everything is kind of working), when I use ToTable("tblname"), it'll quote it, generating invalid sql (for sql server, at least, it'll be 'select * from [Exame]' which doesnt run).

Also, I think this issue should be moved to the other repo and closed after fixing this quoting bug

You should add [ForeignKey("ID_EXAME_STATUS")] to the navigation property (e.g. ExameStatus).

I remember trying that, but didnt work really. The first issue was that it was trying to join Status with Motivo instead of Exame with Motivo, before I used the patches to the latest versions of both libraries.

After the patch, using the code like I posted works ok, it should not ?

@raaffaaeell does it works now?

I didnt reply earlier, but as I said, only works if I use the latest beta7 and apply the patch so FluentMap-Dommel works with the latest version of Dommel