From 7365ffbee6c57dbf3903d4f03b2c0927a1f152c7 Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 30 Sep 2019 14:54:43 +0200 Subject: [PATCH] Fixed init of DbReverseForeignObject generic list detection and extraction --- .../Attributes/DbReverseForeignObject.cs | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Database-Attribute_System/Attributes/DbReverseForeignObject.cs b/Database-Attribute_System/Attributes/DbReverseForeignObject.cs index d9760fa..7a25f26 100644 --- a/Database-Attribute_System/Attributes/DbReverseForeignObject.cs +++ b/Database-Attribute_System/Attributes/DbReverseForeignObject.cs @@ -32,14 +32,31 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes this.classAttribute = classAttribute; this.foreignObjectType = fi.FieldType; - // Init foreign-object class - DbObject foreignClassAttribute = ClassAction.Init(this.foreignObjectType); - if (classAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{classAttribute.parentClassType.Name}' does not have a primaryKey."); if (classAttribute.primaryKeyAttributes.Count > 1) throw new InvalidOperationException($"ReverseForeignObject does not support multiple primaryKeys."); // Get primaryKey name if none is set if (_foreignKeyName == null) _foreignKeyName = classAttribute.primaryKeyAttributes[0]._attributeName; + // If its a List, get inner type + if (!(fi.FieldType.IsGenericType && (fi.FieldType.GetGenericTypeDefinition() == typeof(List<>)))) + { + // Check the generic list and get inner-type + foreach (Type interfaceType in fi.FieldType.GetInterfaces()) + { + if (interfaceType.IsGenericType && + interfaceType.GetGenericTypeDefinition() + == typeof(IList<>)) + { + foreignObjectType = fi.FieldType.GetGenericArguments()[0]; + break; + } + } + if (foreignObjectType == null) throw new InvalidOperationException("Could not read innter-type of generic-list!"); + } + + // Init inner-type class + DbObject foreignClassAttribute = ClassAction.Init(foreignObjectType); + // Check if my primary-key is set in the foreign-class as foreignKey DbPrimaryKey primaryKey = classAttribute.primaryKeyAttributes[0]; foreach (DbForeignKey foreignKey in foreignClassAttribute.foreignKeyAttributes)