Our database has a Primary Key defined on every table existing of a combined key with a short and an int. Therefore, using Entity Framework we can try to find an element by calling to it's Context.DbSet<>.Find(params object[] parameters)
method.
In our code this would look like:
public Client FindClient(short sqlId, int incId)
{
Context db = new Context();
Client result = db.Clients.Find(sqlId, incId);
return result;
}
However, in our code we are using structs to store key values. Our struct is as follows:
public struct DbKey
{
private short _SqlId;
private int _IncId;
public short SqlId { get { return _SqlId; } }
public int IncId { get { return _IncId; } }
public DbKey(short SqlId, int IncId)
{
this._SqlId = SqlId;
this._IncId = IncId;
}
and has some other comparing methods etc. We would like to be able to call the DbSet.Find method like this:
public Client FindClient(DbKey key)
{
Context db = new Context();
Client result = db.Clients.Find(key);
return result;
}
To be able to do this we wrote an extension overload for this method:
public static partial class Extensions
{
public static T Find<T>(this DbSet<T> dbset, DbKey key) where T : class
{
return dbset.Find(key.IncId, key.SqlId);
}
}
Intellisense now tells us the Find method is overloaded, with one version accepting DbKey key
as parameter, and the other, original method accepting params object[] parameters
. However, when running the code, the function will always call to the original method and never to the overloaded, since DbKey also matches the original parameters, resulting in an exception. How can we solve this problem?
Well, you can rename, or call static extension directly: