public async void Missing_primary_key() { using (var testStore = SqliteTestStore.CreateScratch()) { testStore.ExecuteNonQuery("CREATE TABLE Alicia ( Keys TEXT );"); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, ProjectPath = "testout", ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly }); var errorMessage = SqliteDesignStrings.MissingPrimaryKey("Alicia"); var expectedLog = new LoggerMessages { Warn = { errorMessage } }; AssertLog(expectedLog); Assert.Contains(errorMessage, InMemoryFiles.RetrieveFileContents("testout", "Alicia.cs")); } }
public async void Principal_missing_primary_key() { using (var testStore = SqliteTestStore.GetOrCreateShared("NoPrincipalPk" + DbSuffix).AsTransient()) { testStore.ExecuteNonQuery(@"CREATE TABLE Dependent ( Id PRIMARY KEY, PrincipalId INT, FOREIGN KEY (PrincipalId) REFERENCES Principal(Id) ); CREATE TABLE Principal ( Id INT);"); testStore.Transaction.Commit(); var results = await Generator.GenerateAsync(new ReverseEngineeringConfiguration { ConnectionString = testStore.Connection.ConnectionString, ProjectPath = "testout", ProjectRootNamespace = "E2E.Sqlite", UseFluentApiOnly = UseFluentApiOnly }); var expectedLog = new LoggerMessages { Warn = { SqliteDesignStrings.MissingPrimaryKey("Principal"), SqliteDesignStrings.ForeignKeyScaffoldError("Dependent", "PrincipalId") } }; AssertLog(expectedLog); var expectedFileSet = new FileSet(new FileSystemFileService(), Path.Combine(ExpectedResultsParentDir, "NoPrincipalPk")) { Files = { "NoPrincipalPk" + DbSuffix + "Context.expected", "Dependent.expected", "Principal.expected" } }; var actualFileSet = new FileSet(InMemoryFiles, "testout") { Files = Enumerable.Repeat(results.ContextFile, 1).Concat(results.EntityTypeFiles).Select(Path.GetFileName).ToList() }; AssertEqualFileContents(expectedFileSet, actualFileSet); AssertCompile(actualFileSet); } }
private void LoadTablesAndColumns(SqliteConnection connection, ModelBuilder modelBuilder, ICollection <string> tables) { foreach (var tableName in tables) { modelBuilder.Entity(tableName, builder => { builder.ToTable(tableName); var tableInfo = connection.CreateCommand(); tableInfo.CommandText = $"PRAGMA table_info(\"{tableName.Replace("\"", "\"\"")}\");"; var keyProps = new List <string>(); using (var reader = tableInfo.ExecuteReader()) { while (reader.Read()) { var colName = reader.GetString((int)TableInfo.Name); var typeName = reader.GetString((int)TableInfo.Type); var isPk = reader.GetBoolean((int)TableInfo.Pk); var notNull = isPk || reader.GetBoolean((int)TableInfo.NotNull); var clrType = _typeMapper.GetClrType(typeName, nullable: !notNull); var property = builder.Property(clrType, colName) .HasColumnName(colName); if (!string.IsNullOrEmpty(typeName)) { property.HasColumnType(typeName); } var defaultVal = reader.GetValue((int)TableInfo.DefaultValue) as string; if (!string.IsNullOrEmpty(defaultVal)) { property.HasDefaultValueSql(defaultVal); } if (isPk) { keyProps.Add(colName); } else { property.IsRequired(notNull); } } } if (keyProps.Count > 0) { builder.HasKey(keyProps.ToArray()); } else { var errorMessage = SqliteDesignStrings.MissingPrimaryKey(tableName); builder.Metadata.AddAnnotation(AnnotationNameEntityTypeError, errorMessage); Logger.LogWarning(errorMessage); } }); } }