简体   繁体   English

我可以让上下文从抽象类继承并首先使用代码吗?

[英]Can I get my context to inherit from an abstract class and use code first?

So that I could keep my context clean and simple, I put a lot of logic in an abstract class and made my context inherit from it. 为了使上下文保持简洁,我在抽象类中添加了很多逻辑,并使上下文继承自该类。

I saw this approach here but now since my class no longer inherits directly from DBContext I can't create migrations. 我在这里看到了这种方法但是由于我的班级不再直接从DBContext继承, DBContext我无法创建迁移。

My abstract class is 我的抽象课是

public abstract class MyContext : DbContext
{

    public MyContext(string connString)
        : base(connString)
    {
    }
   public override int SaveChanges()
    {
        // custom code here
    }
}

Now when I try and create an migration by typing add-migration at the PM console I get an error indicating that a class inheriting from DBContext cannot be found 现在,当我尝试通过在PM控制台中键入add-migration创建迁移时,出现一个错误,指示找不到从DBContext继承的类

The PM console shows 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'. 

If you search the EF repo for this error message ("No migrations configuration type was found"), you'll find this resource in EntityFramework/Properties/Resources.cs file: 如果您在EF存储库中搜索此错误消息(“未找到迁移配置类型”),则会在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);
}

The next step would be search for AssemblyMigrator_NoConfiguration usage, and you'll find just one occurence which is in EntityFramework/Migrations/Design/ToolingFacade.cs: 下一步是搜索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));
}

I think now it would be easier to track the error and fix it. 我认为现在更容易跟踪错误并进行修复。

I've tested it on Source code and the message was irrelevant, turns out that the rel cause is a target framework tag in app.config. 我已经在源代码上对其进行了测试,并且该消息无关紧要,事实证明,相关原因是app.config中的目标框架标记。

Here is the similar question with right answer: 'Enable-Migrations' fails after upgrading to .NET 4.5 and EF 5 这是带有正确答案的类似问题: 升级到.NET 4.5和EF 5后,“启用迁移”失败

Interesting that If you run Enable-Mirations and Add-Migration pointing exactly to Context class name and Configuratin class name respectively, it works fine. 有趣的是,如果运行Enable-Mirations和Add-Migration分别分别精确指向Context类名和Configuratin类名,则可以正常工作。 But the mentioned solution is the right solution and is easier also :-) 但是提到的解决方案是正确的解决方案,也很容易:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM