Пример #1
0
        async Task SaveInner(IEntity record)
        {
            if (record.IsNew)
            {
                await InsertAsync(record).ConfigureAwait(false);
            }
            else
            {
                await UpdateAsync(record).ConfigureAwait(false);
            };

            if (ManyToManyAssociations.Any())
            {
                await SaveManyToManyRelation(record).ConfigureAwait(false);
            }
        }
Пример #2
0
        async Task SaveManyToManyRelation(IEntity record)
        {
            if (!ManyToManyAssociations.None())
            {
                foreach (var property in ManyToManyAssociations)
                {
                    var inner  = MetaData.GetInnerColumn(property);
                    var outer  = MetaData.GetOuterColumn(property);
                    var bridge = property.BridgeTableName;

                    var command  = "DELETE FROM {0} WHERE {1} = @{2}".FormatWith(bridge, inner, inner);
                    var commands = new List <string> {
                        command
                    };
                    var parameters = new List <IDataParameter> {
                        CreateParameter(inner, record.GetId(), property.DbType)
                    };

                    var objectIdsProperty = record.GetType().GetProperties().FirstOrDefault(x => x.Name == property.Name + "Ids");
                    if (objectIdsProperty == null)
                    {
                        continue;
                    }
                    var objectIdsPropertyList = objectIdsProperty.GetValue(record, null) as IList <string>;

                    var objDataType = property.DbType;

                    for (var i = 0; i < objectIdsPropertyList.Count; i++)
                    {
                        var insertCommand = $"INSERT INTO {bridge} ({inner}, {outer}) VALUES (@{inner}, @{outer}{i})";
                        commands.Add(insertCommand);

                        parameters.Add(CreateParameter(outer + i, objectIdsPropertyList[i], objDataType));
                    }
                    await Db.ExecuteNonQuery(commands.ToString(";"), CommandType.Text, parameters.ToArray()).ConfigureAwait(false);
                }
            }
        }