/// <param name="classType">Type of class to which this is the ForeignKey</param>
/// <param name="dbAttributeName">Name of database-attribute (case-sensitivity is determined from database-attribute-settings) ['null' if the same as field-name]</param>
/// <param name="dbAttributeName">Name of database-attribute (case-sensitivity is determined from database-attribute-settings) ['null' if the same as field-name]</param>
if(foreignClassAttribute.primaryKeyAttributes.Count<1)thrownewInvalidOperationException($"'{foreignClassAttribute.parentClassType.Name}' does not have a primaryKey.");
if(foreignClassAttribute.primaryKeyAttributes.Count>1)thrownewInvalidOperationException($"ForeignObject does not support multiple primaryKeys.");
TypeprimaryKeyType=foreignClassAttribute.primaryKeyAttributes[0].parentField.GetType();// Read type of primaryKey in foreignObject-class
foreach(DbForeignKeyforeignKeyinclassAttribute.foreignKeyAttributes)// Search for matching foreignKey
// If a name was specified and the key does not match its an error
thrownewInvalidOperationException($"ForeignKey='{this._foreignKeyName}' is typeOf='{foreignKey.parentField.GetType().Name}' but primaryKey of foreignObject-class is typeOf='{primaryKeyType.Name}'.");
/// Marks variable as intermediate-object of an dbObject
/// </summary>
/// <param name="intermediateTableName">Table-name of intermediate-table. Must contain primaryKey of this class & target class</param>
/// <param name="keyName">Fieldname of primaryKey associated with the IntermediateObject on this side [m]:n (null if same as primary key) [Only works with 1 primaryKey]</param>
/// <param name="foreignKeyName">Fieldname of primaryKey associated with the IntermediateObject on the other side m:[n] (null if same as primary key) [Only works with 1 primaryKey]</param>
if(classAttribute.primaryKeyAttributes.Count<1)thrownewInvalidOperationException($"'{classAttribute.parentClassType.Name}' does not have a primaryKey.");
if(classAttribute.primaryKeyAttributes.Count>1)thrownewInvalidOperationException($"IntermediateObject does not support multiple primaryKeys.");
thrownewInvalidOperationException($"IntermediateObject has to be typeof(List<T>). Maybe you meant to use DbForeignObject or DbReverseForeignObject for 1:m or 1:1 relations.");
if(foreignClassAttribute.primaryKeyAttributes.Count<1)thrownewInvalidOperationException($"'{foreignClassAttribute.parentClassType.Name}' does not have a primaryKey.");
if(foreignClassAttribute.primaryKeyAttributes.Count>1)thrownewInvalidOperationException($"IntermediateObject does not support multiple primaryKeys. (Found '{foreignClassAttribute.primaryKeyAttributes.Count}' in '{foreignClassAttribute.parentClassType.Name}')");
/// <param name="tableName">Name of database-table (case-sensitivity is determined from database-table-settings) ['null' if the same as class-name]</param>
/// <param name="tableName">Name of database-table (case-sensitivity is determined from database-table-settings) ['null' if the same as class-name]</param>
publicDbObject(stringtableName=null)
publicDbObject(stringtableName=null)
{
{
this._tableName=tableName;// Todo: Automatic resolving of name if it is null (?)
/// <param name="dbAttributeName">Name of database-attribute (case-sensitivity is determined from database-attribute-settings) ['null' if the same as field-name]</param>
/// <param name="dbAttributeName">Name of database-attribute (case-sensitivity is determined from database-attribute-settings) ['null' if the same as field-name]</param>
publicDbPrimaryKey(stringattributeName=null)
publicDbPrimaryKey(stringattributeName=null)
{
{
this._attributeName=attributeName;// Todo: Automatic resolving of name if it is null (?)
/// Marks variable as reverse-foreign-object of an dbObject
/// </summary>
/// <param name="foreignKeyName">Fieldname of primaryKey associated with the reverseForeignObject (null if same as primary key) [Only works with 1 primaryKey]</param>
if(classAttribute.primaryKeyAttributes.Count<1)thrownewInvalidOperationException($"'{classAttribute.parentClassType.Name}' does not have a primaryKey.");
if(classAttribute.primaryKeyAttributes.Count>1)thrownewInvalidOperationException($"ReverseForeignObject does not support multiple primaryKeys.");
if(primaryKey._attributeName.ToLower()==foreignKey._attributeName.ToLower())// Name matches
if(primaryKey.parentField.GetType()==foreignKey.parentField.GetType())// Type matches
{
foreignKeyAttribute=foreignKey;
}
else
// Same name, but wrong type
thrownewInvalidOperationException($"ForeignObject='{foreignClassAttribute.parentClassType.Name}' has invalid type foreignKey='{foreignKey.parentField.Name}' for object='{classAttribute.parentClassType.Name}' with primaryKey='{primaryKey.parentField.Name}'.");
}
// No match
if(foreignKeyAttribute==null)thrownewInvalidOperationException($"ForeignObject='{foreignClassAttribute.parentClassType.Name}' is missing foreignKey for object='{classAttribute.parentClassType.Name}' with primaryKey='{primaryKey.parentField.Name}'.");
if(dbObject.primaryKeyAttributes.Count<1)thrownewInvalidOperationException($"No primaryKey found in '{classType.Name}'");
if(dbObject.primaryKeyAttributes.Count>1)thrownewInvalidOperationException($"This 'GetByPrimaryKey' method only supports 1 primaryKey ('{dbObject.primaryKeyAttributes.Count}' found in '{classType.Name}')");
if(values.Count==0)thrownewInvalidOperationException($"'{reverseForeignObjectAtt.parentField.Name}' could not been resolved. ReverseForeignObject returned '{values.Count}' values.");
// Now scan the just resolved class to be able to set myself
if(reverseForeignObject_typeisIList&&reverseForeignObject_type.IsGenericType)// List, so 1:m
{
reverseForeignObject_value=values;
}
else// Not list, so 1:1
{
if(values.Count>1)thrownewInvalidOperationException($"'{reverseForeignObjectAtt.parentField.Name}' could not been resolved as ReverseForeignObject returned '{values.Count}' values. (Is it 1:m instead of 1:1?)");
if(dbPrimaryKeys.Count==0)thrownewInvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!");
/// <param name="dbAttributes">The db-attributes with dbAttribute-name and value<para/>If null is given, it will generate a default 'SELECT * FROM tableName'</param>