private void GenerateEntities() { _typeRegistry = _dbModel.Driver.TypeRegistry; var viewPrefix = _dbModel.Config.NamingPolicy.ViewPrefix; var tablePrefix = _dbModel.Config.NamingPolicy.TablePrefix; //track uniqueness of type names - we might have trouble if we have 2 tables with the same name in different schemas var typeNames = new StringSet(); foreach (var table in _dbModel.Tables) { if (_config.IgnoreTables.Contains(table.TableName)) { continue; } var module = GetModule(table.Schema); var entName = DbNameToCsName(table.TableName); switch (table.Kind) { case EntityKind.Table: if (string.IsNullOrWhiteSpace(tablePrefix) && entName.StartsWith(tablePrefix)) { entName = entName.Substring(tablePrefix.Length); } break; case EntityKind.View: if (!string.IsNullOrWhiteSpace(viewPrefix) && entName.StartsWith(viewPrefix)) { entName = entName.Substring(viewPrefix.Length); } break; } if (_config.Options.IsSet(DbFirstOptions.ChangeEntityNamesToSingular)) { entName = StringHelper.Unpluralize(entName); } entName = "I" + entName; if (typeNames.Contains(entName)) { entName = entName + "_" + table.Schema; } var entType = CreateDummyEntityType(entName); //dummy type, just to have unique type instance typeNames.Add(entName); // we add only entity types for tables; views are ignored (we do not have queires to create view definitions) if (table.Kind == EntityKind.Table) { module.Entities.Add(entType); // register type in module } // Note: we generate entity interfaces for Views, but do not register them as entities var ent = new EntityInfo(module, entType, table.Kind); ent.TableName = table.TableName; table.Entity = ent; _entityModel.RegisterEntity(ent); // generate entity members foreach (var col in table.Columns) { var nullable = col.Flags.IsSet(DbColumnFlags.Nullable); var memberDataType = GetMemberType(col); var memberName = CheckMemberName(DbNameToCsName(col.ColumnName), ent); var member = col.Member = new EntityMemberInfo(ent, MemberKind.Column, memberName, memberDataType); member.ColumnName = col.ColumnName; // member is added to ent.Members automatically in constructor if (nullable) { member.Flags |= EntityMemberFlags.Nullable; // in case it is not set (for strings) } if (col.Flags.IsSet(DbColumnFlags.Identity)) { member.Flags |= EntityMemberFlags.Identity; member.AutoValueType = AutoType.Identity; } //hack if (col.TypeInfo.VendorDbType.TypeName == "timestamp") { member.AutoValueType = AutoType.RowVersion; } member.Size = (int)col.TypeInfo.Size; member.Scale = col.TypeInfo.Scale; member.Precision = col.TypeInfo.Precision; //Check if we need to specify DbType or DbType spec explicitly bool isMemo = member.Size < 0; if (isMemo) { member.Flags |= EntityMemberFlags.UnlimitedSize; } var typeDef = col.TypeInfo.VendorDbType; var dftTypeDef = _typeRegistry.FindVendorDbTypeInfo(member.DataType, isMemo); if (typeDef == dftTypeDef) { continue; //no need for explicit DbType } /* * bool typeIsDefault = typeDef.ColumnOutType == dataType && typeDef.Flags.IsSet(VendorDbTypeFlags.IsDefaultForClrType); * if (typeIsDefault) * continue; //no need for explicit DbType */ //DbTypeDef is not default for this member - we need to specify DbType or TypeSpec explicitly // Let's see if explicit DbType is enough; let's try to search by DbType and check if it brings the same db type var vendorTypeDef = _typeRegistry.FindVendorDbTypeInfo(col.TypeInfo.VendorDbType.DbType, memberDataType, isMemo); if (vendorTypeDef == typeDef) { member.ExplicitDbType = col.TypeInfo.DbType; //Explicit db type is enough } else { member.ExplicitDbTypeSpec = col.TypeInfo.SqlTypeSpec; } } } //foreach table } //method
private void GenerateEntities() { _typeRegistry = _dbModel.Driver.TypeRegistry; var viewPrefix = _dbModel.Config.NamingPolicy.ViewPrefix; var tablePrefix = _dbModel.Config.NamingPolicy.TablePrefix; //track uniqueness of type names - we might have trouble if we have 2 tables with the same name in different schemas var typeNames = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase); foreach (var table in _dbModel.Tables) { if(_config.IgnoreTables.Contains(table.TableName)) continue; var module = GetModule(table.Schema); var entName = DbNameToCsName(table.TableName); switch(table.Kind) { case EntityKind.Table: if (string.IsNullOrWhiteSpace(tablePrefix) && entName.StartsWith(tablePrefix)) entName = entName.Substring(tablePrefix.Length); break; case EntityKind.View: if (!string.IsNullOrWhiteSpace(viewPrefix) && entName.StartsWith(viewPrefix)) entName = entName.Substring(viewPrefix.Length); break; } if (_config.Options.IsSet(DbFirstOptions.ChangeEntityNamesToSingular)) entName = StringHelper.Unpluralize(entName); entName = "I" + entName; if(typeNames.Contains(entName)) entName = entName + "_" + table.Schema; var entType = CreateDummyEntityType(entName); //dummy type, just to have unique type instance typeNames.Add(entName); // we add only entity types for tables; views are ignored (we do not have queires to create view definitions) if (table.Kind == EntityKind.Table) module.Entities.Add(entType); // register type in module // Note: we generate entity interfaces for Views, but do not register them as entities var ent = new EntityInfo(module, entType, table.Kind); ent.TableName = table.TableName; table.Entity = ent; _entityModel.RegisterEntity(ent); // generate entity members foreach (var col in table.Columns) { var nullable = col.Flags.IsSet(DbColumnFlags.Nullable); var memberDataType = GetMemberType(col); var memberName = CheckMemberName(DbNameToCsName(col.ColumnName), ent); var member = col.Member = new EntityMemberInfo(ent, MemberKind.Column, memberName, memberDataType); member.ColumnName = col.ColumnName; // member is added to ent.Members automatically in constructor if (nullable) member.Flags |= EntityMemberFlags.Nullable; // in case it is not set (for strings) if(col.Flags.IsSet(DbColumnFlags.Identity)) { member.Flags |= EntityMemberFlags.Identity; member.AutoValueType = AutoType.Identity; } //hack if (col.TypeInfo.VendorDbType.TypeName == "timestamp") member.AutoValueType = AutoType.RowVersion; member.Size = (int)col.TypeInfo.Size; member.Scale = col.TypeInfo.Scale; member.Precision = col.TypeInfo.Precision; //Check if we need to specify DbType or DbType spec explicitly bool isMemo = member.Size < 0; if(isMemo) member.Flags |= EntityMemberFlags.UnlimitedSize; var typeDef = col.TypeInfo.VendorDbType; var dftTypeDef = _typeRegistry.FindVendorDbTypeInfo(member.DataType, isMemo); if(typeDef == dftTypeDef) continue; //no need for explicit DbType /* bool typeIsDefault = typeDef.ColumnOutType == dataType && typeDef.Flags.IsSet(VendorDbTypeFlags.IsDefaultForClrType); if (typeIsDefault) continue; //no need for explicit DbType */ //DbTypeDef is not default for this member - we need to specify DbType or TypeSpec explicitly // Let's see if explicit DbType is enough; let's try to search by DbType and check if it brings the same db type var typeDef2 = _typeRegistry.FindVendorDbTypeInfo(col.TypeInfo.VendorDbType.DbType, memberDataType, isMemo); if (typeDef2 == typeDef) member.ExplicitDbType = col.TypeInfo.VendorDbType.DbType; //Explicit db type is enough else member.ExplicitDbTypeSpec = col.TypeInfo.SqlTypeSpec; } }//foreach table }