Last active
August 23, 2019 07:07
-
-
Save meriturva/87a32678ecd59119082b71bde8af80f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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