ConfigureServices
在.NET Core中,我们可以在方法中添加它
services.AddMediatR(typeof(Startup));
Run Code Online (Sandbox Code Playgroud)
但在 .NET 6 中,只有Program.cs
. 如何在.NET 6中添加mediatr?
尝试使用此代码但出现构建错误
当我运行我的程序直到启动 > 配置时,它可以在 C:\Serilog 创建日志文本文件,但它在下面的这一行中断:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
Run Code Online (Sandbox Code Playgroud)
错误是:
Microsoft.AspNetCore.Hosting.Diagnostics[6]
Application startup exception
System.InvalidOperationException: Unable to resolve service for type 'Serilog.Extensions.Hosting.DiagnosticContext' while attempting to activate 'Serilog.AspNetCore.RequestLoggingMiddleware'
Run Code Online (Sandbox Code Playgroud)
这是我的程序.cs:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
namespace xxx
{
public class Program
{
public static void Main(string[] args)
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();
try
{
Log.Information("Application starting up");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "The application failed to start correctly.");
} …
Run Code Online (Sandbox Code Playgroud) 我是react.js的新手.我一直关注https://learnetto.com/blog/how-to-do-simple-form-validation-in-reactjs
但是在index.js中添加bootstrap后,我收到了这个错误
无法编译./src/index.js未找到模块:无法解析'C:\ react-form-validation-demo\src'中的'bootstrap/dist/css/bootstrap-theme.css'
Failed to compile ./src/index.js Module not found: Can't resolve
'bootstrap/dist/css/bootstrap-theme.css' in
'C:\react-form-validation-demo\src'
Run Code Online (Sandbox Code Playgroud)
如果我删除下面这两行,它的工作原理如下:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/css/bootstrap-theme.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
Run Code Online (Sandbox Code Playgroud) 我有一个使用 EF Core 的 ASP.NET Core 3.1 Web API 应用程序。这是我在类的ConfigureServices
方法中的配置Startup
:
services.AddDbContext<ApplicationContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("AppConn")));
Run Code Online (Sandbox Code Playgroud)
上述配置已测试并适用于 SQL Server 数据库。
然后我在成功安装它的包后切换到使用 Sqlite。
services.AddDbContext<ApplicationContext>(options =>
options.UseSqlite("Data Source=sqlitedemo.db"));
Run Code Online (Sandbox Code Playgroud)
但是当我尝试添加 EF 迁移时
add-migration initial -context ApplicationContext
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
System.Reflection.TargetInvocationException:调用的目标已抛出异常。
System.TypeLoadException: 无法从程序集“Microsoft.EntityFrameworkCore, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60”中加载类型“Microsoft.EntityFrameworkCore.Internal.SemanticVersionComparer”。
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor..ctor(IOOperationReportHandler reportHandler, IDictionary args)
--- 内部异常堆栈跟踪结束 ---
在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean 构造函数, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfoculture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfoculture)
at System .Activator.CreateInstance(类型类型,BindingFlags bindingAttr,Binder …
我正在尝试在 Window 中使用 DBeaver (7.3.2) 连接到同一台计算机中的 SQL Server 2019 Express。在我的 SQL Server 2019 Express 中,我成功加载了一个示例 Northwind 数据库。我已经使用用户测试了 SQL Server 2019 中的连接sa
,没有问题。
我已检查SQL Server Configuration Manager
>SQL Server Network Configuration
并启用 TCP/IP。在SQL Native Client 11.0 Configuration
>下Client Protocols
,TCP/IP
默认端口为 1433。
回到DBeaver,当我点击 时Test Connection
,我得到了错误
服务器 SQLEXPRESS 未配置为使用 TCP/IP 进行侦听。
如何从 git Visual Studio 2019 克隆特定分支?我知道如何在命令中执行此操作,但不确定如何使用 Visual Studio 2019 中的新 git 进行操作。
我所能做的就是先克隆主分支,然后从那里选择特定的分支。
我已经安装了 .NET 8 SDK 预览版,但当我在最新的 Visual Studio 2022 版本 17.8.0 Preview 2.0 中创建新项目时,它没有显示。
我也找不到这个Use preview of the .NET SDK
选项Manage Preview Feature
我有这个表,它有 2 列来形成组合键。我正在使用 EF Core。这是我的模型
public class MyModel
{
[Key]
[Column(Order = 0)]
[StringLength(255)]
public string column1 { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(255)]
public string column2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我运行 xunit 测试时,出现此错误
The entity type xxx has multiple properties with the [Key] attribute. Composite primary keys can only be set using 'HasKey' in 'OnModelCreating'.'
Run Code Online (Sandbox Code Playgroud)
这是 xunit 的代码。
public MyServicesTest()
{
var options = new DbContextOptionsBuilder<MyContext>();
options.UseSqlServer(myServiceSqlConnStr);
_myServicesContext = new MyContext(options.Options);
_myServicesContext.Database.EnsureDeleted();
}
Run Code Online (Sandbox Code Playgroud)
错误来自_myServicesContext.Database.EnsureDeleted();
这是我的上下文类 …
我有这个 sql 查询,它在实体框架 6 中使用原始 sql 并返回整数列表。
var sqlString = "SELECT DISTINCT A.ListId FROM ABC A INNER JOIN ABCLists B ON B.Id = A.ListId"
var recordIds = context.Database.SqlQuery<int>(sqlString).ToList();
Run Code Online (Sandbox Code Playgroud)
如何将上面的转换为 Entity Framework Core 3?
我尝试使用 EF Core,但不确定在哪里定义它以列表 int 类型返回。以下是我所拥有的。
var recordIds = _djContext.ABC.FromSqlRaw(sqlString).ToList();
Run Code Online (Sandbox Code Playgroud)
注意:我不需要 LINQ 版本,只需要原始 SQL。谢谢。
我正在使用 XUnit 编写集成测试,我的 Web api 代码也在 C# NET 6 和 EF Core 中。
当我调试它时,它可以到达Web api及其服务层。但当它到达 EF Core 上下文查询示例时private Message? GetMessage() => _myContext.Messages.OrderBy(m => m.CreatedUtc).FirstOrDefault();
,它会在 处中断Program.cs
。
TestingWebAppFactory
这是类的代码
public class TestingWebAppFactory<TEntryPoint> : WebApplicationFactory<Program> where TEntryPoint : Program
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<MyContext>));
if (descriptor != null)
services.Remove(descriptor);
services.AddDbContext<MyContext>(options =>
{
options.UseInMemoryDatabase("myinmemorydb");
});
var sp = services.BuildServiceProvider();
using (var scope = sp.CreateScope())
using (var …
Run Code Online (Sandbox Code Playgroud)