diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs
index c0c2251..c5219f9 100644
--- a/Database-Attribute_System/ClassAction.cs
+++ b/Database-Attribute_System/ClassAction.cs
@@ -73,16 +73,60 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
///
- /// Gets an dbObject
+ /// Gets an dbObject by primaryKey/s
///
///
/// Given object (marked with Db-attributes)
/// Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]
/// This checks if any class-field and data-attribute does not exists in either (Slower)
- public static T GetByPrimaryKey(Type classType, Func>> queryExecutor, bool runDataLossChecks = true) where T: new()
+ public static T GetByPrimaryKey(Type classType, object primaryKeyValue, Func>> queryExecutor, bool runDataLossChecks = true) where T : new()
{
+ Dictionary primaryKeyData = new Dictionary() { };
+ primaryKeyData.Add(null, primaryKeyValue);
+
+ return GetByPrimaryKey(classType, primaryKeyData, queryExecutor, runDataLossChecks);
+ }
+ public static T GetByPrimaryKey(Type classType, string primaryKeyName, object primaryKeyValue, Func>> queryExecutor, bool runDataLossChecks = true) where T : new()
+ {
+ Dictionary primaryKeyData = new Dictionary() { };
+ primaryKeyData.Add(primaryKeyName, primaryKeyValue);
+
+ return GetByPrimaryKey(classType, primaryKeyData, queryExecutor, runDataLossChecks);
+ }
+ public static T GetByPrimaryKey(Type classType, Dictionary primaryKeyData, Func>> queryExecutor, bool runDataLossChecks = true) where T: new()
+ {
+ // Create new empty object
T obj = new T();
+ // Read all fields
+ Dictionary dbFields = Function.ReadDbClassFields(classType);
+ // iterate thru them to check and fill object
+ foreach (KeyValuePair field in dbFields)
+ {
+ // primaryKeys
+ if (field.Value.GetCustomAttribute(typeof(DbPrimaryKey), true) is DbPrimaryKey pkey)
+ {
+ bool dataMatchFound = false;
+
+ // Now search the corresponding primaryKeyData
+ foreach (KeyValuePair primaryKey in primaryKeyData)
+ {
+ // primaryKey matches
+ if(field.Value.Name.ToLower() == primaryKey.Key.ToLower())
+ {
+ // Set data
+ field.Value.SetValue(obj, primaryKey.Value);
+
+ dataMatchFound = true;
+ break;
+ }
+ }
+
+ // If no data was found matching this field
+ if (!dataMatchFound) throw new InvalidOperationException($"Cannot create object with primaryKeyData. No data assigned to field '{field.Value.Name}'");
+ }
+ }
+
string query = QueryBuilder.SelectByPrimaryKey(obj); // Generate query
List> dataSet = queryExecutor(query); // Execute
FillObject(obj, dataSet[0], runDataLossChecks); // Fill the object
@@ -122,7 +166,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
///
- /// Resolves all foreignKeys with the database
+ /// Resolves all foreignKeys with the database
+ /// Only works if the foreignKey is single (not assembled)!
///
///
/// Given object (marked with Db-attributes)
@@ -147,7 +192,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
// When its empty, get it
if(f_value == null)
{
- f_value = GetByPrimaryKey(classType, queryExecutor, runDataLossChecks); ;
+ f_value = GetByPrimaryKey(classType, f_value, queryExecutor, runDataLossChecks); ;
}
// Recursive resolving
diff --git a/Database-Attribute_System/Database-Attribute_System.csproj b/Database-Attribute_System/Database-Attribute_System.csproj
index 6eab338..281ed9c 100644
--- a/Database-Attribute_System/Database-Attribute_System.csproj
+++ b/Database-Attribute_System/Database-Attribute_System.csproj
@@ -4,7 +4,7 @@
netcoreapp2.1
eu.railduction.netcore.dll.Database_Attribute_System
false
- 1.4
+ 1.4.1