Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ IReadOnlyDictionary<string, TypeInfo> Create()
var items
= from t in Assembly.GetConstructibleTypes()
where t.IsSubclassOf(typeof(Migration))
&& t.GetCustomAttribute<DbContextAttribute>()?.ContextType == _contextType
&& t.GetCustomAttribute<DbContextAttribute>(inherit: false)?.ContextType == _contextType
let id = t.GetCustomAttribute<MigrationAttribute>()?.Id
orderby id
select (id, t);
Expand Down Expand Up @@ -94,7 +94,7 @@ public virtual ModelSnapshot? ModelSnapshot
=> _modelSnapshot
??= (from t in Assembly.GetConstructibleTypes()
where t.IsSubclassOf(typeof(ModelSnapshot))
&& t.GetCustomAttribute<DbContextAttribute>()?.ContextType == _contextType
&& t.GetCustomAttribute<DbContextAttribute>(inherit: false)?.ContextType == _contextType
select (ModelSnapshot)Activator.CreateInstance(t.AsType())!)
.FirstOrDefault();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Migrations_ignores_the_unattributed()

var result = assembly.Migrations;

Assert.Equal(2, result.Count);
Assert.Equal(3, result.Count);
Assert.DoesNotContain(result, t => t.GetType() == typeof(MigrationWithoutAttribute));
Assert.Equal(
RelationalResources.LogMigrationAttributeMissingWarning(logger).GenerateMessage(nameof(MigrationWithoutAttribute)),
Expand Down Expand Up @@ -84,4 +84,45 @@ protected override void Up(MigrationBuilder migrationBuilder)
{
}
}

[ConditionalFact]
public void Migrations_handles_inherited_DbContextAttribute()
{
var assembly = CreateInheritedMigrationsAssembly();

// This should not throw AmbiguousMatchException
var result = assembly.Migrations;

Assert.Single(result);
Assert.Contains(result, t => t.Key == "20150302103200_InheritedMigration");
}

private IMigrationsAssembly CreateInheritedMigrationsAssembly()
=> new MigrationsAssembly(
new CurrentDbContext(new DerivedContext()),
new DbContextOptions<DbContext>(
new Dictionary<Type, IDbContextOptionsExtension>
{
{ typeof(FakeRelationalOptionsExtension), new FakeRelationalOptionsExtension() }
}),
new MigrationsIdGenerator(),
new FakeDiagnosticsLogger<DbLoggerCategory.Migrations>());

private class DerivedContext : Context;

[DbContext(typeof(Context)), Migration("20150302103200_BaseMigration")]
private class BaseMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
}

[DbContext(typeof(DerivedContext)), Migration("20150302103200_InheritedMigration")]
private class InheritedMigration : BaseMigration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
}
}
Loading