Fixed GetByPrimaryKey with only primaryKeyValue throwing error because of null-key

master
Railz 6 years ago
parent 3a0af04b1c
commit 5da5d455b9

@ -87,10 +87,13 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param> /// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
public static T GetByPrimaryKey<T>(Type classType, object primaryKeyValue, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new() public static T GetByPrimaryKey<T>(Type classType, object primaryKeyValue, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
{ {
Dictionary<string, object> primaryKeyData = new Dictionary<string, object>() { }; // Read dbObject-attribute
primaryKeyData.Add(null, primaryKeyValue); DbObject dbObject = ClassAction.Init(classType);
return GetByPrimaryKey<T>(classType, primaryKeyData, queryExecutor); if (dbObject.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"No primaryKey found in '{classType.Name}'");
if (dbObject.primaryKeyAttributes.Count > 1) throw new InvalidOperationException($"This 'GetByPrimaryKey' method only supports 1 primaryKey ('{dbObject.primaryKeyAttributes.Count}' found in '{classType.Name}')");
return GetByPrimaryKey<T>(classType, dbObject.primaryKeyAttributes[0]._attributeName, primaryKeyValue, queryExecutor);
} }
public static T GetByPrimaryKey<T>(Type classType, string primaryKeyName, object primaryKeyValue, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new() public static T GetByPrimaryKey<T>(Type classType, string primaryKeyName, object primaryKeyValue, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
{ {
@ -107,6 +110,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
// Read dbObject-attribute // Read dbObject-attribute
DbObject dbObject = ClassAction.Init(classType); DbObject dbObject = ClassAction.Init(classType);
if (dbObject.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"No primaryKey found in '{classType.Name}'");
// iterate thru them to check and fill object // iterate thru them to check and fill object
foreach (DbPrimaryKey primaryKeyAtt in dbObject.primaryKeyAttributes) foreach (DbPrimaryKey primaryKeyAtt in dbObject.primaryKeyAttributes)
{ {

Loading…
Cancel
Save