protected virtual void ConfigureProperty(IMutableProperty property, string configuration, string value) { var propertyAnnotations = property.Relational(); switch (configuration) { case nameof(RelationalPropertyAnnotations.DefaultValue): property.ValueGenerated = ValueGenerated.OnAdd; propertyAnnotations.DefaultValue = value; break; case nameof(RelationalPropertyAnnotations.DefaultValueSql): property.ValueGenerated = ValueGenerated.OnAdd; propertyAnnotations.DefaultValueSql = value; break; case nameof(RelationalPropertyAnnotations.ComputedColumnSql): property.ValueGenerated = ValueGenerated.OnAdd; propertyAnnotations.ComputedColumnSql = value; break; default: throw new NotImplementedException(); } }
private static void SetDateTime2ColumnForDate(IMutableProperty property) { if (property.ClrType == typeof(DateTime)) { property.Relational().ColumnType = "datetime2"; } }
private static void SetDateCreatedInsert(IMutableProperty property) { if (property.Name == "DateCreated") { property.ValueGenerated = ValueGenerated.OnAdd; property.IsStoreGeneratedAlways = true; property.Relational().DefaultValueSql = "GETDATE()"; } }
private static void SetDateUpdatedInsert(IMutableProperty property) { if (property.Name == "DateModified" || property.Name == "DateUpdated") { property.ValueGenerated = Microsoft.EntityFrameworkCore.Metadata.ValueGenerated.OnAddOrUpdate; property.IsStoreGeneratedAlways = true; property.Relational().DefaultValueSql = "GETDATE()"; } }
public static void AddSqlDefaultValue(this ModelBuilder modelBuilder, string propertyName, Type propertyType, string defaultValueSql) { foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes()) { IMutableProperty property = entityType.GetProperties().SingleOrDefault(p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); if (property != null && property.ClrType == propertyType) { property.Relational().DefaultValueSql = defaultValueSql; } } }
/// <summary> /// Set NEWSEQUENTIALID() sql function for all columns named "Id" /// </summary> /// <param name="modelBuilder"></param> /// <param name="mustBeIdentity">Set to true if you want only "Identity" guid fields that named "Id"</param> public static void AddSequentialGuidForIdConvention(this Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder) { const string defaultValueSql = "NEWSEQUENTIALID()"; foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes()) { IMutableProperty property = entityType.GetProperties().SingleOrDefault(p => p.Name.Equals("Id", StringComparison.OrdinalIgnoreCase)); if (property != null && property.ClrType == typeof(Guid)) { property.Relational().DefaultValueSql = defaultValueSql; } } }
private void ConfigureCiText(ModelBuilder modelBuilder, IMutableProperty property) { modelBuilder.HasPostgresExtension("citext"); if (property.PropertyInfo?.PropertyType == typeof(string)) { var columnAttribute = property.PropertyInfo .GetCustomAttributes <ColumnAttribute>() .SingleOrDefault(); if (string.IsNullOrEmpty(columnAttribute?.TypeName)) { property.Relational().ColumnType = "citext"; } } }
protected virtual void ConfigureProperty(IMutableProperty property, string configuration, string value) { var propertyAnnotations = property.Relational(); switch (configuration) { case nameof(RelationalPropertyAnnotations.DefaultValue): property.ValueGenerated = ValueGenerated.OnAdd; propertyAnnotations.DefaultValue = value; break; case nameof(RelationalPropertyAnnotations.DefaultValueSql): property.ValueGenerated = ValueGenerated.OnAdd; propertyAnnotations.DefaultValueSql = value; break; case nameof(RelationalPropertyAnnotations.ComputedColumnSql): property.ValueGenerated = ValueGenerated.OnAdd; propertyAnnotations.ComputedColumnSql = value; break; default: throw new NotImplementedException(); } }
private void MapColumnNames(IMutableProperty property) { var columnName = property.Relational().ColumnName; property.Relational().ColumnName = _nameTranslator.TranslateMemberName(columnName); }