tmsmith / Dapper-Extensions

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple entities map for same table do not work together anymore

ALMMa opened this issue · comments

Affected version: 1.7.0 (this works with 1.6.3).

Consider the table:

MyTable
{
   Id INT,
   Name VARCHAR(100),
   Description VARCHAR(MAX)
}

Now, consider these two POCO entities:

public class MyType
{
   public int Id { get; set; }
   public string Name { get; set; }
   public string Description { get; set; }
}

public class MyTypeSelection
{
   public int Id { get; set; }
   public string Name { get; set; }
}

And the following class maps:

public class MyTypeMap : ClassMap<MyType>
{
   public MyTypeMap()
   {
      Table("MyTable");
      Map(x => x.Id).Column("Id");
      Map(x => x.Name).Column("Name");
      Map(x => x.Description).Column("Description");
   }
}

public class MyTypeSelectionMap : ClassMap<MyTypeSelection>
{
   public MyTypeSelectionMap()
   {
      Table("MyTable");
      Map(x => x.Id).Column("Id");
      Map(x => x.Name).Column("Name");
   }
}

Mixing predicates from one with the other was never an issue before, as they would rely on the destination table to build the SQL command alias and all.

For instance, this would work before and now it gives back the multipart identifier ... could not be bound error:

// on a previous shared code we build a core predicate like
var namePredicate = Predicates.Field<MyTypeSelection>(x => x.Name, Operator.Like, filter);

// And later on another code, we "enhance" that predicate
Predicates.Group(
   GroupOperator.Or,
   Predicates.Field<MyType>(x => x.Description, Operator.Like, filter),
   namePredicate);

Interesting enough, this only happens on the first execution of a query with both mixed. If you ignore the first execution you fall into another issue from table name/alias mapping and suddenly it works.

Though I'm not sure on how much this could impact the community, this can lead to previously shared code infrastructure being rendered useless and upgrading would require a refactor to make explicit chained calls to always have the same POCO types on predicates.

Please be advised.

@ALMMa , I just notice that you started your predicates with MyTypeSelection and then add a predicate of MyType.

Types are more strict now to support self relations and relational properties of the same type of the parent object.

As you have no reference map between both entities it won't find the 2nd predicate data.

Using your example you can add your predicate or create a reference map between the 2 types

Hey @valfrid-ly

Thanks for the reply. So this is intentional, right?
It will require a refactor on my end (and maybe others might face the same thing) so it would be nice to have this well documented (sorry if I missed, but didn't see anything regarding this).

But anyway, if that's the actual intention it's just fine.

I'm sorry.

I'm running to finish postgre unit tests and updating docs.

We are behind on that.