public override TableMetaInfo GetTableMetaInfo(Type type) { IModel model = db.Model; IEntityType entityType = model.FindEntityType(type); IEntityType[] allEntityTypes = model.GetEntityTypes().ToArray(); IProperty[] entityProperties = entityType.GetProperties().ToArray(); IRelationalEntityTypeAnnotations mapping = entityType.Relational(); EFPropertyInfo[] pProperties = new EFPropertyInfo[entityProperties.Length]; // Column info for (int i = 0; i < entityProperties.Length; i++) // (var property in entityProperties) { { IProperty prop = entityProperties[i]; IRelationalPropertyAnnotations rProp = prop.Relational(); if (rProp == null || rProp.ColumnName.IsNulle()) { continue; } bool isIndex = prop.IsIndex(); var idx1 = prop.GetContainingKeys().ToArray(); EFPropertyInfo pInfo = new EFPropertyInfo() { EntityName = prop.Name, ColumnName = rProp.ColumnName, IsIdentity = false, IsKey = prop.IsKey(), IsIndex = isIndex, IsPrimaryKey = prop.IsPrimaryKey() }; string columnType = rProp.ColumnType; pProperties[i] = pInfo; } ; var info = new TableMetaInfo() { TableName = mapping.TableName, //table.Table, // watch out! not table.Name TableSchema = mapping.Schema, //table.Schema, TypeNameFull = entityType.Name, // or: == type.FullName, TypeName = type.Name, TableColumnNames = pProperties.Select(p => p.ColumnName).Where(n => n.NotNulle()).ToArray(), //declaredProps.Select(dp => dp.Name).ToArray(), EntityPropertyNames = pProperties.Select(p => p.EntityName).Where(n => n.NotNulle()).ToArray(), //mapped.Select(dd => dd.Name).ToArray() KeyColumnNames = pProperties.Where(p => p != null && p.IsKey).Select(p => p.ColumnName).ToArray(), // table.ElementType.KeyMembers.Select(m => m.Name).ToArray(), }; return(info); }
private static string GetTableName <T>(this DbContext context) { IRelationalEntityTypeAnnotations entityTypeAnnotations = context.Model .FindEntityType(typeof(T)) .Relational(); string schema = entityTypeAnnotations.Schema; string table = entityTypeAnnotations.TableName; return(string.IsNullOrWhiteSpace(schema) ? $"[{table}]" : $"[{schema}].[{table}]"); }
public virtual async Task UpsertDtoAsync <TDto>(TDto dto) // https://github.com/aspnet/EntityFrameworkCore/issues/9249 where TDto : class, IDto { if (dto == null) { throw new ArgumentNullException(nameof(dto)); } if (((IsSyncDbContext)this).IsSyncDbContext == false && dto is ISyncableDto syncableDto) { Entry(syncableDto).Property("IsSynced").CurrentValue = false; } TypeInfo dtoType = dto.GetType().GetTypeInfo(); IEntityType efCoreDtoType = Model.FindEntityType(dtoType); IRelationalEntityTypeAnnotations dtoTypeRelationalInfo = efCoreDtoType.Relational(); string tableName = $"[{dtoTypeRelationalInfo.TableName}]"; // No schema support var props = efCoreDtoType.GetProperties() .Select(p => new { p.Name, Value = Entry(dto).Property(p.Name).CurrentValue }) .ToArray(); foreach (INavigation nav in efCoreDtoType.GetNavigations()) { if (nav.ClrType.GetCustomAttribute <ComplexTypeAttribute>() == null) { continue; } object navInstance = nav.PropertyInfo.GetValue(dto); if (navInstance == null) { continue; } props = props.Union(nav.ClrType.GetProperties() .Select(p => new { Name = $"{nav.Name}_{p.Name}", Value = p.GetValue(navInstance) })) .ToArray(); } string sql = $"INSERT OR REPLACE INTO {tableName} ({string.Join(",", props.Select(p => $"[{p.Name}]"))}) VALUES({string.Join(",", props.Select((p, i) => $"{{{i}}}"))})"; await Database.ExecuteSqlCommandAsync(sql, props.Select(p => p.Value)).ConfigureAwait(false); }
} // End Function GetAsSelectList public List <string> ListTableNames() { List <string> lsTables = new List <string>(); // var mapping = _context.Model.FindEntityType(typeof(string)).Relational(); // string schema = mapping.Schema; // string tableName = mapping.TableName; foreach (IEntityType et in this.m_ctx.Model.GetEntityTypes()) { IRelationalEntityTypeAnnotations rel = et.Relational(); // string schema = rel.Schema; string table = rel.TableName; // System.Console.WriteLine(table, schema); lsTables.Add(table); } // Next et return(lsTables); } // End Function ListTableNames
private static string Format(IRelationalEntityTypeAnnotations annotations) => (string.IsNullOrEmpty(annotations.Schema) ? "" : annotations.Schema + ".") + annotations.TableName;
public static DatabaseColumn GetColumn(this DatabaseModel model, IRelationalEntityTypeAnnotations table, IRelationalPropertyAnnotations column) { return(GetColumn(model, table.Schema, table.TableName, column.ColumnName)); }
public static bool ColumnExists(this DatabaseModel model, IRelationalEntityTypeAnnotations table, IRelationalPropertyAnnotations column) { return(ColumnExists(model, table.Schema, table.TableName, column.ColumnName)); }
public static DatabaseTable GetTable(this DatabaseModel model, IRelationalEntityTypeAnnotations table) { return(GetTable(model, table.Schema ?? model.DefaultSchema, table.TableName)); }
public static bool TableExists(this DatabaseModel model, IRelationalEntityTypeAnnotations table) { return(TableExists(model, table.Schema ?? model.DefaultSchema, table.TableName)); }
public string GetTableName() { IRelationalEntityTypeAnnotations relationalEntityType = _entityType.Relational(); return($"[{relationalEntityType.Schema}].[{relationalEntityType.TableName}]"); }
public static string FormSchemaTable(this IRelationalEntityTypeAnnotations eRel) { return(FormSchemaTable(eRel.Schema, eRel.TableName)); }