小编Tho*_*hou的帖子

无法创建类型为“DbContext”的对象

当我尝试跑步时

dotnet ef migration add Init
Run Code Online (Sandbox Code Playgroud)

我得到错误

无法创建类型为“IdentityContext”的对象。

我知道是什么导致了这个问题。我想学习使用消息总线并将其添加到我的项目中,所以我参加了一些课程并实施了它。Ofc 总线工作正常。但问题是我无法再通过 EF 进行迁移。

我之前的 Startup.cs 是。

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}
Run Code Online (Sandbox Code Playgroud)

现在看起来像这样。

public static void Main(string[] args)
{
  ServiceHost.Create<Startup>(args)
    .UseRabbitMq()
    .Build()
    .Run();
}
Run Code Online (Sandbox Code Playgroud)

和 ServiceHost 类

public void Run() => _webHost.Run();

public static HostBuilder Create<TStartup>(string[] args) where TStartup : class
{
  Console.Title = typeof(TStartup).Namespace;
  var config = new ConfigurationBuilder()
    .AddEnvironmentVariables()
    .AddCommandLine(args)
    .Build();
  var webHostBuilder = …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework asp.net-core

15
推荐指数
7
解决办法
3万
查看次数

Oracle ORA-00942: 表或视图存在时不存在

我正在尝试习惯 Oracle,安装了 express one 并由第 3 部分程序创建了一些表。当我登录到 sqlplus 时,我不能简单地使用 SELECT * FROM table ....

SQL> SELECT * FROM tab;

TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
ZIP                            TABLE
Country                        TABLE
City                           TABLE
Run Code Online (Sandbox Code Playgroud)

但是当我尝试选择所有它运行时:

SQL> SELECT * FROM Country;
SELECT * FROM Country
              *
ERROR at line 1:
ORA-00942: table or view does not exist
Run Code Online (Sandbox Code Playgroud)

我不知道为什么...

sql oracle quoted-identifier

3
推荐指数
1
解决办法
1万
查看次数

查看会话变量

前段时间我创建了一些简单的登录会话。这是一个 MVC 应用程序,但如果我是对的,它是 .net framework 4.6。我可以在那里使用类似的东西

<h2>@Session["ID"]</h2>
Run Code Online (Sandbox Code Playgroud)

来自会话变量的 ID 应该在 h2 标签中。但是现在我尝试使用 .net core 2.0 构建相同的版本。

我的startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddDbContext<L2Context>(options =>
            options.UseSqlite("Data Source=test.db"));

    services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache
    services.AddSession(options => {
        options.IdleTimeout = TimeSpan.FromMinutes(30);
    });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();
    app.UseSession();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}
Run Code Online (Sandbox Code Playgroud)

控制器将数据保存到会话中:

[HttpPost]
public IActionResult Login(Users user)
{
    var …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core

1
推荐指数
1
解决办法
3732
查看次数

无法解析范围服务

我在理解代码中的错误源时遇到了问题。我尝试着讲有关.net核心中的微服务的课程。运行构建解决方案后,我得到:

------- Project finished: CrossX.Services.Identity. Succeeded: True. Errors: 0. Warnings: 0
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我得到:

/opt/dotnet/dotnet /RiderProjects/crossx/src/CrossX.Services.Identity/bin/Debug/netcoreapp2.2/CrossX.Services.Identity.dll

Unhandled Exception: System.InvalidOperationException: Cannot resolve scoped service 'CrossX.NETCore.Commands.ICommandHandler`1[CrossX.NETCore.Commands.CreateUser]' from root provider.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, IServiceScope scope, IServiceScope rootScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at CrossX.NETCore.Services.ServiceHost.BusBuilder.SubscribeToCommand[TCommand]() in /RiderProjects/crossx/src/CrossX.NETCore/Services/ServiceHost.cs:line 78
   at CrossX.Services.Identity.Program.Main(String[] args) in /RiderProjects/crossx/src/CrossX.Services.Identity/Program.cs:line 11
Run Code Online (Sandbox Code Playgroud)

当我添加到webHostBuilder .UseDefaultServiceProvider(options => options.ValidateScopes = false)时,我的问题已解决。但是据我所知,关闭验证并不是一个好主意。另外,当我将AddScope更改为AddTransient时,问题已解决(或至少已运行)。

问题是我不知道在哪里可以找到此错误的来源。我想我不太了解哪里出了问题,因此,如果有人可以帮助我,或者至少提供一个提示,我将不胜感激。

这是我的

Startup.cs:

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called …
Run Code Online (Sandbox Code Playgroud)

c# asp.net .net-core asp.net-core

1
推荐指数
1
解决办法
2016
查看次数