/// <summary> /// Create a new instance into database. /// </summary> /// <param name="instance">Instance to create.</param> /// <returns>The instance unique identifier.</returns> private static long InsertDatabaseRecord(T instance) { object value; ORMSqlCommand cmd = ORMEntity <T> .SqlDialect.GetInsertCommand(); // Connecto to database ORMEntity <T> .Connect(); // Set command parameters foreach (ORMEntityMember param in cmd.Parameters) { value = param.GetValue(instance); ORMEntity <T> .SetParameter(param, value is null?DBNull.Value : value); } // Execute the SELECT sentence to retrieve the instance properties ORMEntity <T> .ExecuteNonQuery(cmd.SqlCommand); long id = ExecuteScalar(SqlDialect.GetNewIDCommand().SqlCommand); // Set the generated new record unique identifier to the current instance ORMEntity <T> .ORMStructure.PrimaryKey.SetValue(instance, id); // Close the connection to database ORMEntity <T> .Disconnect(); // Created project is added to in-memory table ORMEntity <T> .AddInMemoryTable(id, instance); return(((ORMIdentifiableEntity)instance).ID); }
/// <summary> /// Maps the database readed data into a new instance. /// </summary> private static T MapData(DbDataReader reader) { T instance; object value; try { // Create the new instance instance = (T)Activator.CreateInstance(typeof(T), new object[] { }); // Add primary key ORMEntity <T> .ORMStructure.PrimaryKey.SetValue(instance, reader); // After object is identified, add it to the memory table ORMEntity <T> .AddInMemoryTable(((ORMIdentifiableEntity)instance).ID, instance); foreach (ORMEntityMember member in ORMEntity <T> .ORMStructure) { value = null; if (member.IsForeignField) { value = member.GetReaderValue(instance, reader); member.SetValue(instance, value); } else if (member.IsForeignCollection) { value = ORMEntity <T> .GetORMForeignCollection(member, instance); member.SetValue(instance, value); } else { member.SetValue(instance, reader); } if (member.Attribute.IsOneToOneProperty) { ORMEntity <T> .SetOneToOneProperty(member.Attribute.OneToOnePropertyName, value, instance); } } return(instance); } catch (Exception ex) { Logger.LogError(ex); throw ex; } }