我下面就如何在做一個通用存儲庫ASP.Net核心這篇文章/教程:當我下載我能夠運行遷移和項目運行和工程代碼 http://www.c-sharpcorner.com/article/generic-repository-pattern-in-asp-net-core/當我的Model和Repository在類庫中時,如何在.Net Core中使用DotNet遷移?
。
但是當我遵循的方向,並嘗試從頭構建,然後當我得到的地步,我必須執行的遷移:
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」中添加一個無參數構造函數,或者在與「ApplicationContext」相同的程序集中添加一個「IDbContextFactory」實現。
本教程有在類庫中定義的模型和回購這包括:
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>());
}
}
}
的選項從StartUp.cs通過在Web項目是這樣的:
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項目中的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> = 1.1.0但收到1.0.0'ApplicationContext'上找不到無參數構造函數。或者在「ApplicationContext」中添加一個無參數構造函數,或者在與「ApplicationContext」相同的程序集中添加一個「IDbContextFactory」實現。
下載如何工作以及如何讓我的工作。 如此困惑。 最重要的是,我不得不永久地使用EF Core和Tools Preview上的所有依賴關係,才能獲得這麼好的效果。
是否有更好的文章介紹如何做到這一點?
我固定: 「Microsoft.Extensions.Logging」: 「1.0.0」,
到 「Microsoft.Extensions.Logging」: 「1.1.0」,
和現在我得到這個錯誤:
System.TypeLoadException:Method'Apply'in'Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationalColumnAttributeConvention'from assembly'Microsoft.EntityFrameworkCore.Relational,Version = 1.0.0.0,Culture =中性,PublicKeyToken = adb9793829ddae60'沒有實現。在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。在類型<> c__DisplayClass0_0.b__0() 在Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(字串[] args) 在Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(字串[] args) 方法 '應用'程序集「Microsoft.EntityFrameworkCore.Relational,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60」中的'Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationalColumnAttributeConvention'沒有實現。
http://stackoverflow.com/questions/42213583/net-core-entity-framework-add-migration-for-context-in-class-library – Alexan
http://stackoverflow.com/questions/41433129/entity -framework核遷移換ASP淨核心類庫 – Alexan