当我的模型和存储库位于类库中时,如何在.Net Core中使用DotNet Migrations?

时间:2017-02-16 18:27:36

标签: entity-framework-core ef-migrations

我正在关注如何为ASP.Net Core制作通用存储库的这篇文章/教程: http://www.c-sharpcorner.com/article/generic-repository-pattern-in-asp-net-core/

当我下载代码时,我能够运行迁移,项目运行并运行。

但是当我按照指示尝试从头开始构建时,那么当我到达必须运行迁移的时候:

Add-migration MyFirstMigration

我收到此错误:

C:\ Tutorials \ ASP.Net \ Core \ GenericRepository \ FromScratch \ GenericReposotory \ GR.Web \ project.json(20,43):警告NU1012:依赖冲突。 Microsoft.EntityFrameworkCore 1.1.0期望Microsoft.Extensions.Logging> = 1.1.0但收到1.0.0在' ApplicationContext'上找不到无参数构造函数。将无参数构造函数添加到' ApplicationContext'或者添加' IDbContextFactory'的实现在与ApplicationContext'相同的程序集中。

本教程在类库中定义了模型和repo,包括:

namespace GR.Data
{
    public class ApplicationContext : DbContext
    {
        public ApplicationContext(DbContextOptions<ApplicationContext> options)
        : base(options) { }

        protected override void OnModelCreating(ModelBuilder modelBuilder){
        base.OnModelCreating(modelBuilder);
        new AuthorMap(modelBuilder.Entity<Author>());
        new BookMap(modelBuilder.Entity<Book>());
        }
    }
}

选项从Web项目的StartUp.cs传入,如下所示:

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);
        services.AddMvc();

        services.AddDbContext<ApplicationContext>(
            options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
    }

它从Web Project中的appsettings.json获取连接字符串:GR.Web。

在软件包管理器控制台中,我选择了GR.Data作为默认项目,并将GR.Web设置为启动项目。我收到这个错误:

C:\ Tutorials \ ASP.Net \ Core \ GenericRepository \ FromScratch \ GenericReposotory \ GR.Web \ project.json(20,43):警告NU1012:依赖冲突。 Microsoft.EntityFrameworkCore 1.1.0期望Microsoft.Extensions.Logging&gt; = 1.1.0但收到1.0.0在&#39; ApplicationContext&#39;上找不到无参数构造函数。将无参数构造函数添加到&#39; ApplicationContext&#39;或者添加&#39; IDbContextFactory&#39;的实现在与ApplicationContext&#39;相同的程序集中。

如何下​​载工作以及如何让我的工作。 如此迷茫。 最重要的是,我不得不永远愚弄它,以便在正确的版本上获得EF Core和Tools Preview的所有依赖项,以实现这一目标。

有关如何做到这一点的更好的文章吗?

我修好了: &#34; Microsoft.Extensions.Logging&#34;:&#34; 1.0.0&#34;,

要 &#34; Microsoft.Extensions.Logging&#34;:&#34; 1.1.0&#34;,

现在我收到了这个错误:

System.TypeLoadException:Method&#39; Apply&#39;在类型&#39; Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationalColumnAttributeConvention&#39;来自assembly&#39; Microsoft.EntityFrameworkCore.Relational,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60&#39;没有实施。在Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationalConventionSetBuilder.AddConventions(ConventionSet conventionSet)    在Microsoft.EntityFrameworkCore.Metadata.Conventions.SqlServerConventionSetBuilder.AddConventions(ConventionSet conventionSet)     在Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext上下文,IConventionSetBuilder conventionSetBuilder,IModelValidator验证器)    在System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func 2 valueFactory)     在Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()     在Microsoft.EntityFrameworkCore.Internal.LazyRef 1.get_Value() at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String environment, Action 1记者)     在Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand。&lt;&gt; c__DisplayClass0_0.b__0()     在Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String [] args)     在Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String [] args)  方法&#39;申请&#39;在类型&#39; Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationalColumnAttributeConvention&#39;来自assembly&#39; Microsoft.EntityFrameworkCore.Relational,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60&#39;没有实施。

1 个答案:

答案 0 :(得分:-1)

在Web项目的依赖项中,GR.Web,我回滚了

"Microsoft.EntityFrameworkCore": "1.1.0"

"Microsoft.EntityFrameworkCore": "1.0.1"

现在可行。