当我想开火时我遇到了问题add-migration
ASP MVC Core 2 项目的命令。
尚未注册类型“Microsoft.AspNetCore.Hosting.IHostingEnvironment”的服务。
这是我的启动.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<SampleArchContext>((serviceProvider, optionsBuilder) =>
{
optionsBuilder.UseInternalServiceProvider(serviceProvider); // It's added to access services from the dbcontext, remove it if you are using the normal `AddDbContext` and normal constructor dependency injection.
});
services.AddMvc(options =>
{
options.AllowEmptyInputInBodyModelBinding = true;
}).AddJsonOptions(jsonOptions =>
{
jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
}
public void Configure(
ILoggerFactory loggerFactory,
IApplicationBuilder app,
IHostingEnvironment env)
{
if (env.IsDevelopment())
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "bower_components")),
RequestPath = "/bower_components",
EnableDirectoryBrowsing = false
});
}
}
程序.cs:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.SetBasePath(env.ContentRootPath);
config.AddInMemoryCollection(new[]
{
new KeyValuePair<string,string>("the-key", "the-value")
})
.AddJsonFile("appsettings.json", reloadOnChange: true, optional: false)
.AddJsonFile($"appsettings.{env}.json", optional: true)
.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddDebug();
logging.AddConsole();
})
.UseIISIntegration()
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
})
.UseStartup<Startup>()
.Build();
host.Run();
}
}
context:
public interface IContext
{
DbSet<Person> Persons { get; set; }
DbSet<Country> Countries { get; set; }
DbSet<TEntity> Set<TEntity>() where TEntity : class;
EntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
int SaveChanges();
}
public class SampleArchContext : DbContext, IContext
{
public SampleArchContext(DbContextOptions<SampleArchContext> options)
: base(options)
{ }
public DbSet<Person> Persons { get; set; }
public DbSet<Country> Countries { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Country>(build =>
{
build.Property(category => category.Name).HasMaxLength(450).IsRequired();
});
}
public override int SaveChanges()
{
var modifiedEntries = ChangeTracker.Entries()
.Where(x => x.Entity is IAuditableEntity
&& (x.State == EntityState.Added || x.State == EntityState.Modified));
foreach (var entry in modifiedEntries)
{
IAuditableEntity entity = entry.Entity as IAuditableEntity;
if (entity != null)
{
string identityName = Thread.CurrentPrincipal.Identity.Name;
DateTime now = DateTime.UtcNow;
if (entry.State == EntityState.Added)
{
entity.CreatedBy = identityName;
entity.CreatedDate = now;
}
else
{
base.Entry(entity).Property(x => x.CreatedBy).IsModified = false;
base.Entry(entity).Property(x => x.CreatedDate).IsModified = false;
}
entity.UpdatedBy = identityName;
entity.UpdatedDate = now;
}
}
return base.SaveChanges();
}
}
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<SampleArchContext>
{
public SampleArchContext CreateDbContext(string[] args)
{
var service = new ServiceCollection();
service.AddOptions();
//service.AddScoped<IHostingEnvironment, CustomHostingEnvironment>();
service.AddSingleton<ILoggerFactory, LoggerFactory>();
var serviceProvider = service.BuildServiceProvider();
var hostingEnvirement = serviceProvider.GetRequiredService<IHostingEnvironment>();
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.SetBasePath(basePath: hostingEnvirement.ContentRootPath)
//.SetBasePath(Directory.GetCurrentDirectory())
.Build();
service.AddSingleton(provider => configuration);
serviceProvider = service.BuildServiceProvider();
var builder = new DbContextOptionsBuilder<SampleArchContext>();
var connectionString = configuration.GetConnectionString("AppDbContextConnection");
builder.UseSqlServer(connectionString);
return new SampleArchContext(builder.Options);
}
}
这是我的appsetting.json :
应用程序设置.json:
{
"ConnectionStrings": {
"AppDbContextConnection": "Server=localhost; Database=CoreDbName;Trusted_Connection=True; MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
我查了很多资料都没有找到解决这个问题的方法,请问如何解决呢?