private void MapColumn(string identity, TableMapping tableMapping, EdmProperty property) { var columnName = property.Name; EdmMember edmMember = _tableColumnEdmMembers[identity].Single(c => c.Name == columnName); string propertyName = edmMember.Name; ColumnMapping columnMapping = tableMapping.AddColumn(propertyName, columnName); BuildColumnMapping(identity, property, propertyName, columnMapping); }
private void MapTable(EntityType tableEdmType) { string identity = tableEdmType.FullName; EdmType baseEdmType = tableEdmType; EntitySet storageEntitySet = null; while (!_codeFirstEntityContainer.TryGetEntitySetByName(baseEdmType.Name, false, out storageEntitySet)) { if (baseEdmType.BaseType == null) break; baseEdmType = baseEdmType.BaseType; } if (storageEntitySet == null) return; var tableName = (string)storageEntitySet.MetadataProperties["Table"].Value; var schemaName = (string)storageEntitySet.MetadataProperties["Schema"].Value; var tableMapping = new TableMapping(identity, schemaName, tableName); _tableMappings.Add(identity, tableMapping); _primaryKeysMapping.Add(identity, storageEntitySet.ElementType.KeyMembers.Select(x => x.Name).ToList()); foreach (var prop in storageEntitySet.ElementType.Properties) { MapColumn(identity, _tableMappings[identity], prop); } }
private static DataTable BuildDataTable <T>(TableMapping tableMapping) { var entityType = typeof(T); string tableName = string.Join(@".", tableMapping.SchemaName, tableMapping.TableName); var dataTable = new DataTable(tableName); var primaryKeys = new List <DataColumn>(); foreach (var columnMapping in tableMapping.Columns) { var propertyInfo = entityType.GetProperty(columnMapping.PropertyName, '.'); columnMapping.Type = propertyInfo.PropertyType; var dataColumn = new DataColumn(columnMapping.ColumnName); Type dataType; if (propertyInfo.PropertyType.IsNullable(out dataType)) { dataColumn.DataType = dataType; dataColumn.AllowDBNull = true; } else { dataColumn.DataType = propertyInfo.PropertyType; dataColumn.AllowDBNull = columnMapping.Nullable; } if (columnMapping.IsIdentity) { dataColumn.Unique = true; if (propertyInfo.PropertyType == typeof(int) || propertyInfo.PropertyType == typeof(long)) { dataColumn.AutoIncrement = true; } else { continue; } } else { dataColumn.DefaultValue = columnMapping.DefaultValue; } if (propertyInfo.PropertyType == typeof(string)) { dataColumn.MaxLength = columnMapping.MaxLength; } if (columnMapping.IsPk) { primaryKeys.Add(dataColumn); } dataTable.Columns.Add(dataColumn); } dataTable.PrimaryKey = primaryKeys.ToArray(); return(dataTable); }