I am following this article/tutorial on how to make a generic repository for ASP.Net Core at: http://www.c-sharpcorner.com/article/generic-repository-pattern-in-asp-net-core/
When I download the code I am able to run the migrations and the project runs and works.
But when I follow the directions and try to build from scratch, then when I get to the point where I have to run the migrations:
Add-migration MyFirstMigration
I get this error:
C:\Tutorials\ASP.Net\Core\GenericRepository\FromScratch\GenericReposotory\GR.Web\project.json(20,43): warning NU1012: Dependency conflict. Microsoft.EntityFrameworkCore 1.1.0 expected Microsoft.Extensions.Logging >= 1.1.0 but received 1.0.0 No parameterless constructor was found on 'ApplicationContext'. Either add a parameterless constructor to 'ApplicationContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ApplicationContext'.
The tutorial had the model and repo defined in a class library including this:
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>());
}
}
}
The options are passed in from StartUp.cs in the Web Project like this:
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<>));
}
And it gets the connection string from appsettings.json in the Web Project: GR.Web.
In the Package Manager Console I have GR.Data selected as the default project and GR.Web set as the startup project. I get this error:
C:\Tutorials\ASP.Net\Core\GenericRepository\FromScratch\GenericReposotory\GR.Web\project.json(20,43): warning NU1012: Dependency conflict. Microsoft.EntityFrameworkCore 1.1.0 expected Microsoft.Extensions.Logging >= 1.1.0 but received 1.0.0 No parameterless constructor was found on 'ApplicationContext'. Either add a parameterless constructor to 'ApplicationContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ApplicationContext'.
How is the download working and how do I get mine to work. so confused. On top of it I had to fool around with it forever to get all the depenanceies on EF Core and Tools Preview on the right versions to get this far.
Is there a better article somewhere on how to do this?
I fixed: "Microsoft.Extensions.Logging": "1.0.0",
to "Microsoft.Extensions.Logging": "1.1.0",
and now I get this error:
System.TypeLoadException: Method 'Apply' in type 'Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationalColumnAttributeConvention' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation. at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationalConventionSetBuilder.AddConventions(ConventionSet conventionSet)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.SqlServerConventionSetBuilder.AddConventions(ConventionSet conventionSet)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func
2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.LazyRef1.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 reporter)
at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.b__0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
Method 'Apply' in type 'Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationalColumnAttributeConvention' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not ha ve an implementation.