protected PropertyBaseMapping(ITableMappingCache cache, IColumnMapping foreignKey, string joinOn, PropertyInfo info) { Name = info.Name; JoinOnFieldName = joinOn; ForeignKeyColumn = foreignKey; TableMappingCache = cache; }
public void SetUp() { theMapping = new TestCsvMapping(); theRawValues = new[] { "Test", "true", "1" }; theValues = theMapping.ValueSource(new CsvData(theRawValues)); }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public static string ToDebugString( [NotNull] this IColumnMapping columnMapping, MetadataDebugStringOptions options, [NotNull] string indent = "") { var builder = new StringBuilder(); builder.Append(indent); var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; if (singleLine) { builder.Append($"ColumnMapping: "); } builder.Append(columnMapping.Property.Name).Append(" - "); builder.Append(columnMapping.Column.Name); if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) { builder.Append(columnMapping.AnnotationsToDebugString(indent + " ")); } return(builder.ToString()); }
private static void AddMaxLengthConstraintIfNecessary(IColumnMapping property, ColumnStatement columnStatement) { if (property.MaxLength.HasValue) { columnStatement.ColumnConstraints.Add(new MaxLengthConstraint(property.MaxLength.Value)); } }
/// <summary> /// <para> /// Creates a human-readable representation of the given metadata. /// </para> /// <para> /// Warning: Do not rely on the format of the returned string. /// It is designed for debugging only and may change arbitrarily between releases. /// </para> /// </summary> /// <param name="columnMapping"> The metadata item. </param> /// <param name="options"> Options for generating the string. </param> /// <param name="indent"> The number of indent spaces to use before each new line. </param> /// <returns> A human-readable representation. </returns> public static string ToDebugString( [NotNull] this IColumnMapping columnMapping, MetadataDebugStringOptions options, int indent = 0) { var builder = new StringBuilder(); var indentString = new string(' ', indent); builder.Append(indentString); var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0; if (singleLine) { builder.Append("ColumnMapping: "); } builder .Append(columnMapping.Property.DeclaringEntityType.DisplayName()) .Append(".") .Append(columnMapping.Property.Name) .Append(" - "); builder .Append(columnMapping.Column.Table.Name) .Append(".") .Append(columnMapping.Column.Name); if (!singleLine && (options & MetadataDebugStringOptions.IncludeAnnotations) != 0) { builder.Append(columnMapping.AnnotationsToDebugString(indent + 2)); } return(builder.ToString()); }
public void SetUp() { theMapping = new TestCsvMapping(); theHeaders = new CsvData(new[] { "Count", "Flag", "Name" }); theRawValues = new CsvData(new[] { "1", "true", "Test" }); theValues = theMapping.ValueSource(theRawValues, theHeaders); }
public void SetUp() { theMapping = new MappingWithAliases(); theHeaders = new CsvData(new[] { "Count", "Flag", "SomethingElse" }); theRawValues = new CsvData(new[] { "1", "true", "Test" }); theValues = theMapping.ValueSource(theRawValues, theHeaders); }
private static void AddDefaultValueConstraintIfNecessary(IColumnMapping property, ColumnStatement columnStatement) { if (!string.IsNullOrEmpty(property.DefaultValue)) { columnStatement.ColumnConstraints.Add(new DefaultValueConstraint { DefaultValue = property.DefaultValue }); } }
/// <summary> /// Add a custom mapping /// </summary> /// <param name="mapping">Mapping which is used.</param> public void Add(IColumnMapping mapping) { if (mapping == null) { throw new ArgumentNullException("mapping"); } _mappings.Add(mapping); }
private static void AddNullConstraintIfNecessary(IColumnMapping property, ColumnStatement columnStatement) { // TJT: Review this if (property.IsRequired) { // Only mark it as NotNull if it should not be generated. columnStatement.ColumnConstraints.Add(new NotNullConstraint()); } }
/// <summary>Adds a property/field to the mapping.</summary> /// <typeparam name="T">The <see cref="Type"/> associated with the column mapping.</typeparam> /// <param name="columnMapping"> /// An instance of <see cref="IColumnMapping{T}"/> that has been added to the current mapping. /// </param> /// <param name="propertyName"> /// An <see cref="Expression{T}"/> selecting the property/field to add to the mapping. /// </param> /// <returns> /// An instance of <see cref="PropertyMapping{T}"/> mapped to the property/field /// specified in the <paramref name="propertyName"/> parameter. /// </returns> /// <exception cref="ArgumentNullException"> /// Thrown if the <paramref name="columnMapping"/> or /// <paramref name="propertyName"/> parameter is <c>null</c>. /// </exception> public static PropertyMapping <T> MapProperty <T>(this IColumnMapping <T> columnMapping, Expression <Func <T, object> > propertyName) { if (columnMapping == null) { throw new ArgumentNullException(nameof(columnMapping)); } return(columnMapping.MappedType.MapProperty(propertyName)); }
private static void AddCollationConstraintIfNecessary(IColumnMapping property, ColumnStatement columnStatement) { // FIXME: //var value = property.GetCustomAnnotation<CollateAttribute>(); //if (value != null) //{ // columnStatement.ColumnConstraints.Add(new CollateConstraint { CollationFunction = value.Collation, CustomCollationFunction = value.Function }); //} }
private static void AdjustDatatypeForAutogenerationIfNecessary(IColumnMapping property, ColumnStatement columnStatement) { // FIXME: //if (property.StoreGeneratedPattern == StoreGeneratedPattern.Identity) //{ // // Must be INTEGER else SQLite will not generate the Ids // ConvertIntegerType(columnStatement); //} }
void IMappedType <T> .DefineColumnMapping(IColumnMapping <T> columnMapping) { if (MappedColumns.ContainsKey(columnMapping.ColumnName)) { MappedColumns[columnMapping.ColumnName] = columnMapping.PropertyName; } else { MappedColumns.Add(columnMapping.ColumnName, columnMapping.PropertyName); } }
private static void AddUniqueConstraintIfNecessary(IColumnMapping property, ColumnStatement columnStatement) { var value = property.IsUnique; if (property.IsUnique) { var conflictAction = ConflictAction.None; columnStatement.ColumnConstraints.Add(new UniqueConstraint { ConflictAction = conflictAction }); } }
/// <summary> /// /// </summary> /// <param name="cache"></param> /// <param name="foreignKey"></param> /// <param name="info"></param> /// <param name="name"> Property name </param> public PropertyListMapping(ITableMappingCache cache, IColumnMapping foreignKey, string joinOn, PropertyInfo info) : base(cache, foreignKey, joinOn, info) { try { Getter = (Func <TEntity, ICollection <TProperty> >)Delegate.CreateDelegate(typeof(Func <TEntity, ICollection <TProperty> >), info.GetGetMethod()); Setter = (Action <TEntity, ICollection <TProperty> >)Delegate.CreateDelegate(typeof(Action <TEntity, ICollection <TProperty> >), info.GetSetMethod()); } catch (Exception) { // Hide exception here, the result will be checked later } }
private void AddPrimaryKeyConstraintAndAdjustTypeIfNecessary(IColumnMapping property, ColumnStatement columnStatement) { // Only handle a single primary key this way. if (keyMembers.Count() != 1 || !property.Equals(keyMembers.Single())) { return; } ConvertIntegerType(columnStatement); var primaryKeyConstraint = new PrimaryKeyConstraint(); // FIXME: primaryKeyConstraint.Autoincrement = property.Autoincrement() != null; columnStatement.ColumnConstraints.Add(primaryKeyConstraint); }
private static void CopyAttributeToMapping(ColumnAttribute attribute, IColumnMapping mapping) { if (attribute == null) { return; } if (attribute.IsDbGenerated) { mapping.DbGenerated(); } if (attribute.IsPrimaryKey) { mapping.PrimaryKey(); } if (attribute.IsVersion) { mapping.Version(); } if (!attribute.CanBeNull) { mapping.NotNull(); } if (!string.IsNullOrEmpty(attribute.DbType)) { mapping.DbType(attribute.DbType); } if (!string.IsNullOrEmpty(attribute.Name)) { mapping.Named(attribute.Name); } if (!string.IsNullOrEmpty(attribute.Expression)) { mapping.Expression(attribute.Expression); } if (!string.IsNullOrEmpty(attribute.Storage)) { mapping.Storage(attribute.Storage); } mapping.AutoSync(attribute.AutoSync); mapping.UpdateCheck(attribute.UpdateCheck); //TODO: Discriminator }
private static void CopyAttributeToMapping(ColumnAttribute attribute, IColumnMapping mapping) { if (attribute == null) return; if (attribute.IsDbGenerated) mapping.DbGenerated(); if (attribute.IsPrimaryKey) mapping.PrimaryKey(); if (attribute.IsVersion) mapping.Version(); if (!attribute.CanBeNull) mapping.NotNull(); if (!string.IsNullOrEmpty(attribute.DbType)) mapping.DbType(attribute.DbType); if (!string.IsNullOrEmpty(attribute.Name)) mapping.Named(attribute.Name); if (!string.IsNullOrEmpty(attribute.Expression)) mapping.Expression(attribute.Expression); if (!string.IsNullOrEmpty(attribute.Storage)) mapping.Storage(attribute.Storage); mapping.AutoSync(attribute.AutoSync); mapping.UpdateCheck(attribute.UpdateCheck); //TODO: Discriminator }
public void Setup() { mapping = new ColumnMapping(typeof(Customer).GetProperty("Surname")); }
/// <summary> /// /// </summary> /// <param name="cache"></param> /// <param name="foreignKey"></param> /// <param name="info"></param> /// <param name="name"> Property name </param> public PropertyMapping(ITableMappingCache cache, IColumnMapping foreignKey, string joinOn, PropertyInfo info) : base(cache, foreignKey, joinOn, info) { Getter = (Func <TEntity, TProperty>)Delegate.CreateDelegate(typeof(Func <TEntity, TProperty>), info.GetGetMethod()); Setter = (Action <TEntity, TProperty>)Delegate.CreateDelegate(typeof(Action <TEntity, TProperty>), info.GetSetMethod()); }
public void Setup() { mapping = new ColumnMapping(typeof(Customer).GetProperty("Surname")); }
public void Add(IColumnMapping<TEntity> column) { if (!string.IsNullOrEmpty(column?.FieldName)) base[column.FieldName.ToLower()] = column; }
private static string ColumnName(IColumnMapping column) { return(column.Name); }
public ColAssert(ColsAssert colsAssert, IColumnMapping columnMapping) { And = colsAssert; _columnMapping = columnMapping; }
private static IValueSource valueSourceFor(IEnumerable <string> values, CsvData headers, IColumnMapping mapping) { var data = new CsvData(values); return(headers == null ? mapping.ValueSource(data) : mapping.ValueSource(data, headers)); }
private void InitColumnMapping(Type entityType) { var navProps = new Dictionary <string, PropertyInfo>(); var invProps = new List <KeyValuePair <string, PropertyInfo> >(); // Create properties mappings var properties = entityType.GetProperties(); foreach (var p in properties) { // Get field attributes var columnKey = false; var fieldName = p.Name; var columnName = fieldName; var columnGeneratedByDb = (bool?)null; var columnIsRowVersion = false; var isNotMapped = false; foreach (var a in p.GetCustomAttributes(true)) { var attrType = a.GetType(); if (attrType == typeof(NotMappedAttribute)) { isNotMapped = true; break; } else if (attrType == typeof(ColumnAttribute)) { columnName = (a as ColumnAttribute).Name; } else if (attrType == typeof(KeyAttribute)) { columnKey = true; } else if (attrType == typeof(RowVersionAttribute)) { columnIsRowVersion = true; } else if (attrType == typeof(DatabaseGeneratedAttribute)) { columnGeneratedByDb = (a as DatabaseGeneratedAttribute).DatabaseGeneratedOption != DatabaseGeneratedOption.None; } else if (attrType == typeof(ForeignKeyAttribute)) { navProps[(a as ForeignKeyAttribute).Name] = p; } else if (attrType == typeof(InversePropertyCollectionAttribute)) { invProps.Add(new KeyValuePair <string, PropertyInfo>((a as InversePropertyCollectionAttribute).Property, p)); } } if (!isNotMapped) { var column = GetColumnMapping(p, columnName, fieldName); if (column != null) { mColumns.Add(column); column.IsRowVersion = columnIsRowVersion; column.IsReadonly = (columnGeneratedByDb ?? false) || columnIsRowVersion; if (columnKey || columnName.Equals(Settings?.DefaultPrimaryKeyName ?? "Id", StringComparison.InvariantCultureIgnoreCase)) { PrimaryKey = column; column.IsReadonly = columnGeneratedByDb ?? true; } if (!column.IsReadonly) { mWriteColumns.Add(column); } } } } // Init column index mColumns.SetupColumnIndexes(); // Adding navigation properties foreach (var kvp in navProps) { var mapType = GetPropertyMapping(kvp.Value.PropertyType); var foreignKey = mColumns.GetColumn(kvp.Key); var propInst = (IPropertyMapping <TEntity>)Activator.CreateInstance(mapType, TableMappingCache.Instance, foreignKey, string.Empty, kvp.Value); mProperties.Add(propInst); } foreach (var kvp in invProps) { var mapType = GetPropertyMapping(kvp.Value.PropertyType); var propInst = (IPropertyMapping <TEntity>)Activator.CreateInstance(mapType, TableMappingCache.Instance, PrimaryKey, kvp.Key, kvp.Value); mProperties.Add(propInst); } }
private static string TableName(IColumnMapping column) { return(string.IsNullOrEmpty(column.TableSchema) ? column.TableName : $"{column.TableSchema}.{column.TableName}"); }