Skip to content

Instantly share code, notes, and snippets.

@meriturva
Last active August 23, 2019 07:07
Show Gist options
  • Save meriturva/87a32678ecd59119082b71bde8af80f4 to your computer and use it in GitHub Desktop.
Save meriturva/87a32678ecd59119082b71bde8af80f4 to your computer and use it in GitHub Desktop.
protected virtual Type[] GetKeysTypes()
{
var model = Context.Set<TEntity>().GetService<IDbContextServices>().Model;
var entityType = model.FindEntityType(typeof(TEntity).ToString());
return entityType.FindPrimaryKey()
.Properties
.Select(p => p.ClrType)
.ToArray();
}
public TEntity GetByEntityId(string entityId)
{
var types = this.GetKeysTypes();
var strKeys = entityId.Split('$');
if(types.Length != strKeys.Length)
{
throw new Exception("Error on entityId");
}
var objKeys = new object[strKeys.Length];
for(int i = 0; i < objKeys.Length; i++)
{
objKeys[i] = Convert.ChangeType(strKeys[i], types[i]);
}
return Context.Set<TEntity>().Find(objKeys);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment