Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RevEng: Add DatabaseModelFactoryOptions #15090

Merged
merged 5 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public class DatabaseOperations

var scaffoldedModel = scaffolder.ScaffoldModel(
connectionString,
tables,
schemas,
new DatabaseModelFactoryOptions(tables, schemas),
_rootNamespace,
modelNamespace,
contextNamespace,
Expand Down
7 changes: 2 additions & 5 deletions src/EFCore.Design/Scaffolding/IReverseEngineerScaffolder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using JetBrains.Annotations;

namespace Microsoft.EntityFrameworkCore.Scaffolding
Expand All @@ -15,8 +14,7 @@ public interface IReverseEngineerScaffolder
/// Scaffolds a model from a database schema.
/// </summary>
/// <param name="connectionString"> A connection string to the database. </param>
/// <param name="tables"> A list of tables to include. Empty to include all tables. </param>
/// <param name="schemas"> A list of schemas to include. Empty to include all schemas. </param>
/// <param name="options"> The options specifying which metadata to read from the database. </param>
/// <param name="rootNamespace"> The namespace of the project. </param>
/// <param name="modelNamespace"> The namespace for model classes. </param>
/// <param name="contextNamespace"> The namespace for context class. </param>
Expand All @@ -28,8 +26,7 @@ public interface IReverseEngineerScaffolder
/// <returns> The scaffolded model. </returns>
ScaffoldedModel ScaffoldModel(
[NotNull] string connectionString,
[NotNull] IEnumerable<string> tables,
[NotNull] IEnumerable<string> schemas,
[NotNull] DatabaseModelFactoryOptions options,
[NotNull] string rootNamespace,
[NotNull] string modelNamespace,
[NotNull] string contextNamespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public class ReverseEngineerScaffolder : IReverseEngineerScaffolder
/// </summary>
public virtual ScaffoldedModel ScaffoldModel(
string connectionString,
IEnumerable<string> tables,
IEnumerable<string> schemas,
DatabaseModelFactoryOptions options,
string rootNamespace,
string modelNamespace,
string contextNamespace,
Expand All @@ -80,8 +79,7 @@ public class ReverseEngineerScaffolder : IReverseEngineerScaffolder
ModelCodeGenerationOptions codeOptions)
{
Check.NotEmpty(connectionString, nameof(connectionString));
Check.NotNull(tables, nameof(tables));
Check.NotNull(schemas, nameof(schemas));
Check.NotNull(options, nameof(options));
Check.NotEmpty(modelNamespace, nameof(modelNamespace));
Check.NotEmpty(contextNamespace, nameof(contextNamespace));
Check.NotNull(modelOptions, nameof(modelOptions));
Expand All @@ -101,7 +99,7 @@ public class ReverseEngineerScaffolder : IReverseEngineerScaffolder
codeOptions.SuppressConnectionStringWarning = true;
}

var databaseModel = _databaseModelFactory.Create(resolvedConnectionString, tables, schemas);
var databaseModel = _databaseModelFactory.Create(resolvedConnectionString, options);
var model = _factory.Create(databaseModel, modelOptions.UseDatabaseNames);

if (model == null)
Expand Down
39 changes: 39 additions & 0 deletions src/EFCore.Relational/Scaffolding/DatabaseModelFactoryOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Scaffolding
{
/// <summary>
/// Specifies which metadata to read from the database.
/// </summary>
public class DatabaseModelFactoryOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseModelFactoryOptions" /> class.
/// </summary>
/// <param name="tables"> A list of tables to include. Empty to include all tables. </param>
/// <param name="schemas"> A list of schemas to include. Empty to include all schemas. </param>
public DatabaseModelFactoryOptions([NotNull] IEnumerable<string> tables, [NotNull] IEnumerable<string> schemas)
{
Check.NotNull(tables, nameof(tables));
Check.NotNull(schemas, nameof(schemas));

Tables = tables;
Schemas = schemas;
}

/// <summary>
/// Gets the list of tables to include. If empty, include all tables.
/// </summary>
public IEnumerable<string> Tables { get; }

/// <summary>
/// Gets the list of schemas to include. If empty, include all schemas.
/// </summary>
public IEnumerable<string> Schemas { get; }
}
}
11 changes: 4 additions & 7 deletions src/EFCore.Relational/Scaffolding/IDatabaseModelFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Data.Common;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
Expand All @@ -19,19 +18,17 @@ public interface IDatabaseModelFactory
/// for the database.
/// </summary>
/// <param name="connectionString"> The connection string for the database to reverse engineer. </param>
/// <param name="tables"> The tables to include in the model, or an empty enumerable to include all. </param>
/// <param name="schemas"> The schema to include in the model, or an empty enumerable to include all. </param>
/// <param name="options"> The options specifying which metadata to read. </param>
/// <returns> The database model. </returns>
DatabaseModel Create([NotNull] string connectionString, [NotNull] IEnumerable<string> tables, [NotNull] IEnumerable<string> schemas);
DatabaseModel Create([NotNull] string connectionString, [NotNull] DatabaseModelFactoryOptions options);

/// <summary>
/// Connects to the database using the given connection and creates a <see cref="DatabaseModel" />
/// for the database.
/// </summary>
/// <param name="connection"> The connection to the database to reverse engineer. </param>
/// <param name="tables"> The tables to include in the model, or an empty enumerable to include all. </param>
/// <param name="schemas"> The schema to include in the model, or an empty enumerable to include all. </param>
/// <param name="options"> The options specifying which metadata to read. </param>
/// <returns> The database model. </returns>
DatabaseModel Create([NotNull] DbConnection connection, [NotNull] IEnumerable<string> tables, [NotNull] IEnumerable<string> schemas);
DatabaseModel Create([NotNull] DbConnection connection, [NotNull] DatabaseModelFactoryOptions options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ private void ValidateContextNameInReverseEngineerGenerator(string contextName)
Assert.Throws<ArgumentException>(
() => reverseEngineer.ScaffoldModel(
"connectionstring",
/* tables: */ Enumerable.Empty<string>(),
/* schemas: */ Enumerable.Empty<string>(),
new DatabaseModelFactoryOptions(
/* tables: */ Enumerable.Empty<string>(),
/* schemas: */ Enumerable.Empty<string>()),
"FakeNamespace",
"FakeNamespace",
"FakeNamespace",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public virtual void Clean(DatabaseFacade facade)
{
var databaseModelFactory = CreateDatabaseModelFactory(loggerFactory);
var databaseModel = databaseModelFactory.Create(
connection.DbConnection, Enumerable.Empty<string>(), Enumerable.Empty<string>());
connection.DbConnection,
new DatabaseModelFactoryOptions(Enumerable.Empty<string>(), Enumerable.Empty<string>()));

var operations = new List<MigrationOperation>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public SqliteDatabaseModelFactoryTest(SqliteDatabaseModelFixture fixture)
.BuildServiceProvider()
.GetRequiredService<IDatabaseModelFactory>();

var databaseModel = databaseModelFactory.Create(Fixture.TestStore.ConnectionString, tables, schemas);
var databaseModel = databaseModelFactory.Create(
Fixture.TestStore.ConnectionString,
new DatabaseModelFactoryOptions(tables, schemas));
Assert.NotNull(databaseModel);
asserter(databaseModel);
}
Expand Down