Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IN clause doesn't support SqlMapper.TypeHandler #1134

Open
wanton7 opened this issue Sep 27, 2018 · 0 comments
Open

IN clause doesn't support SqlMapper.TypeHandler #1134

wanton7 opened this issue Sep 27, 2018 · 0 comments

Comments

@wanton7
Copy link

wanton7 commented Sep 27, 2018

I'm using Dapper v1.50.5 from NuGet and I have this struct

public struct ItemGroupId : System.IEquatable<ItemGroupId>
{
  public static readonly ItemGroupId Empty = new ItemGroupId(0);

  private readonly int _value;

  public ItemGroupId(int value) => _value = value;

  public static explicit operator ItemGroupId(int value) => new ItemGroupId(value);

  public static explicit operator int(ItemGroupId value) => value._value;

  public override bool Equals(object obj) => obj is ItemGroupId ? this.Equals((ItemGroupId)obj) : false;

  public bool Equals(ItemGroupId other) => _value == other._value;

  public override int GetHashCode() => _value.GetHashCode();

  public static bool operator ==(ItemGroupId left, ItemGroupId right) => left.Equals(right);

  public static bool operator !=(ItemGroupId left, ItemGroupId right) => !(left == right);

  public override string ToString() => _value.ToString();
}

and this TypeHandler

public class ItemGroupIdTypeHandler : SqlMapper.TypeHandler<ItemGroupId>
{
  public override void SetValue(IDbDataParameter parameter, ItemGroupId value)
  {
    parameter.DbType = DbType.Int32;
    parameter.Value = value != ItemGroupId.Empty ? (object)(int)value : DBNull.Value;
  }

  public override ItemGroupId Parse(object value)
  {
    return new ItemGroupId((int?)value ?? 0);
  }
}

TypeHandler seems to work everywhere but when I try to use ItemGroupId in IN clause like this
sqb.Where("EXISTS (SELECT * FROM ItemForItemGroup WHERE ItemGroupId IN @ItemGroupIds AND ItemId = item.Id)", new { ItemGroupIds = sOptions.ItemGroupIds.ToList() });
I get error System.ArgumentException : No mapping exists from object type Removed.ItemGroupId to a known managed provider native type.

I had to change it to sqb.Where("EXISTS (SELECT * FROM ItemForItemGroup WHERE ItemGroupId IN @ItemGroupIds AND ItemId = item.Id)", new { ItemGroupIds = sOptions.ItemGroupIds.Select(x => (int)x).ToList() }); to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant