因此我可以保持上下文干净简单,我在抽象类中添加了很多逻辑,并使我的上下文继承自它。
我看到了这种方法here,但现在我的班级不再直接从DBContext
继承,我无法创建迁移。
我的抽象类是
public abstract class MyContext : DbContext
{
public MyContext(string connString)
: base(connString)
{
}
public override int SaveChanges()
{
// custom code here
}
}
现在,当我尝试通过在PM控制台上键入add-migration来创建迁移时,我收到一条错误,指示无法找到继承自DBContext
的类
PM控制台显示
PM> add-migration kirsten2 No migrations configuration type was found in the assembly 'DataLayer'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
PM> Enable-Migrations No context type was found in the assembly 'DataLayer'.
答案 0 :(得分:1)
如果您在EF repo中搜索此错误消息(“未找到迁移配置类型”),您将在EntityFramework / Properties / Resources.cs文件中找到此资源:
/// <summary>
/// A string like "No migrations configuration type was found in the assembly '{0}'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration)."
/// </summary>
internal static string AssemblyMigrator_NoConfiguration(object p0)
{
return EntityRes.GetString(EntityRes.AssemblyMigrator_NoConfiguration, p0);
}
下一步是搜索AssemblyMigrator_NoConfiguration
用法,你会发现
只有一个出现在EntityFramework / Migrations / Design / ToolingFacade.cs中:
private DbMigrationsConfiguration FindConfiguration()
{
var configurationType = FindType<DbMigrationsConfiguration>(
ConfigurationTypeName,
types => types
.Where(
t => t.GetConstructor(Type.EmptyTypes) != null
&& !t.IsAbstract
&& !t.IsGenericType)
.ToList(),
Error.AssemblyMigrator_NoConfiguration,
(assembly, types) => Error.AssemblyMigrator_MultipleConfigurations(assembly),
Error.AssemblyMigrator_NoConfigurationWithName,
Error.AssemblyMigrator_MultipleConfigurationsWithName);
return configurationType.CreateInstance<DbMigrationsConfiguration>(
Strings.CreateInstance_BadMigrationsConfigurationType,
s => new MigrationsException(s));
}
我认为现在跟踪错误并修复它会更容易。
我在源代码上测试了它并且消息无关紧要,结果是rel原因是app.config中的目标框架标记。
这是类似的问题,正确答案: 'Enable-Migrations' fails after upgrading to .NET 4.5 and EF 5
有趣的是,如果运行Enable-Mirations和Add-Migration分别指向Context类名和Configuratin类名,它可以正常工作。 但提到的解决方案是正确的解决方案,也更容易: - )