ASP.NET核心发布错误AddDbContext
我实际上不知道为局域网服务器部署IIS。我们正在和我的朋友一起做这个项目。我们共享同一个无线网络。所以我们想要共享api项目。因为我在后端(API-DAL-BLL)层工作,所以我的朋友在前端工作。但我不能很好地部署。 首先,我的发布无法看到我的DBContext。因此,我添加了Startup addDbContext 我的Startup.cs就是这样的ASP.NET核心发布错误AddDbContext,asp.net,entity-framework,asp.net-core,iis,asp.net-apicontroller,Asp.net,Entity Framework,Asp.net Core,Iis,Asp.net Apicontroller,我实际上不知道为局域网服务器部署IIS。我们正在和我的朋友一起做这个项目。我们共享同一个无线网络。所以我们想要共享api项目。因为我在后端(API-DAL-BLL)层工作,所以我的朋友在前端工作。但我不能很好地部署。 首先,我的发布无法看到我的DBContext。因此,我添加了Startup addDbContext 我的Startup.cs就是这样的 public void ConfigureServices(IServiceCollection services) {
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<GastroDB>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<IMainCategoryService, MainCategoryService>();
}
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseUrls("myIpAdress:80").UseIISIntegration().UseStartup<Startup>();
});
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
services.addScope();
}
像这样的My Program.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<GastroDB>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<IMainCategoryService, MainCategoryService>();
}
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseUrls("myIpAdress:80").UseIISIntegration().UseStartup<Startup>();
});
}
publicstaticvoidmain(字符串[]args)
{
CreateHostBuilder(args.Build().Run();
}
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseURL(“MyiPAddress:80”).UseIISIntegration().UseStartup();
});
}
但是iis无法启动。这是我的错误
Description:由于未处理的异常,进程已终止。
异常信息:System.ArgumentException:使用配置调用了“AddDbContext”,但上下文类型“GastroDB”仅声明无参数构造函数。这意味着将永远不会使用传递给“AddDbContext”的配置。如果将配置传递给“AddDbContext”,则“DB”应声明一个接受DbContextOptions的构造函数,并且必须将其传递给DbContext的基本构造函数。
2) iisaspnetcore模块V2
Application '/LM/W3SVC/1/ROOT' with physical root 'C:\Users\Tuğçe\Desktop\almanya projesi BE\EcommerceGastro.API\bin\Release\net5.0\publish\' hit unexpected managed exception, exception code = '0xe0434352'. First 30KB characters of captured stdout and stderr logs:
Unhandled exception. System.ArgumentException: 'AddDbContext' was called with configuration, but the context type 'GastroDB' only declares a parameterless constructor. This means that the configuration passed to 'AddDbContext' will never be used. If configuration is passed to 'AddDbContext', then 'GastroDB' should declare a constructor that accepts a DbContextOptions<GastroDB> and must pass it to the base constructor for DbContext.
Application'/LM/W3SVC/1/ROOT'和物理根'C:\Users\Tuğğe\Desktop\almanya projesi BE\ecommercegasro.API\bin\Release\net5.0\publish\'遇到意外的托管异常,异常代码='0xe0434352'。捕获的stdout和stderr日志的前30KB字符:
未处理的异常。System.ArgumentException:使用配置调用了“AddDbContext”,但上下文类型“GastroDB”仅声明无参数构造函数。这意味着将永远不会使用传递给“AddDbContext”的配置。如果将配置传递给“AddDbContext”,则“DB”应声明一个接受DbContextOptions的构造函数,并且必须将其传递给DbContext的基本构造函数。
我知道我应该像这样添加上下文构造函数
public GastroDB(DbContextOptions<GastroDB> options):base(options){}
using (var transaction = new GastroDB())
{
var productList = transaction.Set<DBProduct>().Include(x => x.ProductImage).ToList();
return this.mapper.Map<List<Product>>(productList);
}
public数据库(DbContextOptions选项):基本(选项){}
但我无法添加,因为在DBContext上第一次这样开始:
public class GastroDB : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("server=.; database=GastroDB; user id=sa; password=123;");
}
private static GastroDB _dbInstance;
public static GastroDB DBInstance
{
get
{
if (_dbInstance == null)
{
_dbInstance = new GastroDB();
}
return _dbInstance;
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new MyMap());
...
}
public DbSet<MyClass> MyClass{ get; set; }
....
}
}
公共类数据库:DbContext
{
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
optionsBuilder.UseSqlServer(“服务器=;数据库=数据库;用户id=sa;密码=123;”);
}
私有静态数据库_dbInstance;
公共静态数据库实例
{
得到
{
if(_dbInstance==null)
{
_dbInstance=newdb();
}
返回_dbInstance;
}
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
ApplyConfiguration(newmymap());
...
}
公共数据库集MyClass{get;set;}
....
}
}
如果我在这段代码中添加了ctor,我的一些服务会抛出exeption,因为我这样使用
public GastroDB(DbContextOptions<GastroDB> options):base(options){}
using (var transaction = new GastroDB())
{
var productList = transaction.Set<DBProduct>().Include(x => x.ProductImage).ToList();
return this.mapper.Map<List<Product>>(productList);
}
使用(var transaction=new-db())
{
var productList=transaction.Set().Include(x=>x.ProductImage.ToList();
返回此.mapper.Map(productList);
}
我不知道如何解决这个问题。请帮帮我
DbContextOptions
公共静态数据库实例
,因为
DbContext
被注册为作用域
生存期
公共类数据库:DbContext
{
公共数据库(DbContextOptions)
:基本(选项)
{
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
optionsBuilder.UseSqlServer(“服务器=;数据库=数据库;用户id=sa;密码=123;”);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
ApplyConfiguration(newmymap());
...
}
公共数据库集MyClass{get;set;}
....
}
}
三,。如果您想将DbContext
注册为singleton
生命周期,您可以使用下面的代码,而不是在DbContext
services.AddDbContext<GastroDB>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
}, ServiceLifetime.Singleton);
services.AddDbContext(选项=>
{
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”))
},servicelifest.Singleton);