小编Que*_*n3r的帖子

QueryFailedError:插入或更新表“graph”违反外键约束“FK_0e40......”

我创建了一个非常小的复制存储库

还请查看下面我可能的修复方法。


我有一个图形和一个图形节点实体。图只知道它的起始图节点,节点本身知道它属于哪个图及其后继图。

@Entity()
export class Graph extends BaseEntity {
  @PrimaryGeneratedColumn("uuid")
  public id: string;

  @Column({ nullable: true })
  public startNodeId?: string;

  @ManyToOne(() => GraphNode)
  @JoinColumn({ name: "startNodeId" })
  public startNode?: GraphNode;
}

@Entity()
export class GraphNode extends BaseEntity {
  @PrimaryGeneratedColumn("uuid")
  public id: string;

  @PrimaryColumn("uuid")
  public graphId: string;

  @ManyToOne(() => Graph, { onDelete: "CASCADE" })
  @JoinColumn({ name: "graphId" })
  public graph: Graph;

  @Column({ nullable: true })
  public successorGraphNodeId?: string;

  @ManyToOne(() => GraphNode)
  @JoinColumn({ name: "successorGraphNodeId" })
  public successorGraphNode?: GraphNode; …
Run Code Online (Sandbox Code Playgroud)

postgresql typeorm nestjs

7
推荐指数
0
解决办法
622
查看次数

每个程序集调用一次 AddAutoMapper 而不是传入多个程序集?

我有一个带有 Web API 项目和库项目的多层项目。这两个项目都依赖于 AutoMapper(以及 Microsoft.Extensions.DependencyInjection 的 AutoMapper 扩展)。基于此

https://docs.automapper.org/en/latest/Dependency-injection.html#asp-net-core

在启动文件中,我正在为所有图层设置 AutoMapper

Assembly apiAssembly = Assembly.GetExecutingAssembly();
Assembly myLibraryAssembly = Assembly.Load("MyLibrary");

services.AddAutoMapper(apiAssembly, myLibraryAssembly);
Run Code Online (Sandbox Code Playgroud)

正如您在此处看到的,API 项目需要通过名称加载它们来了解所有引用的库项目。我更喜欢每个项目都能够自行注册的方式。基于此示例代码

https://github.com/jasontaylordev/CleanArchitecture/blob/master/src/Application/DependencyInjection.cs

我在我的库项目中创建了这样一个文件

public static class DependencyInjection
{
    public static IServiceCollection AddMyLibrary(this IServiceCollection services)
    {
        Assembly executingAssembly = Assembly.GetExecutingAssembly(); // MyLibrary assembly

        services.AddAutoMapper(executingAssembly);
        // ... setup other services

        return services;
    }
}
Run Code Online (Sandbox Code Playgroud)

在 API 项目中,我现在可以做到这一点

Assembly executingAssembly = Assembly.GetExecutingAssembly();

services.AddAutoMapper(executingAssembly);
services.AddMyLibrary();
Run Code Online (Sandbox Code Playgroud)

该代码似乎工作正常,但AddAutoMapper会被调用两次。一次用于 API 程序集,一次用于库程序集。我应该坚持第一种方法,因为 AutoMapper 应该只添加一次还是可以将其分开?

c# automapper

7
推荐指数
2
解决办法
1350
查看次数

如果不应该将 MigrateAsync() 用于生产环境,如何应用 EF Core 迁移?

我创建了一个新的 .Net 5 项目并想使用 EF Core。我使用自动生成了多个 migration.cs 文件

dotnet ef migrations add MyMigration

并希望应用它们(用于开发和生产)。我知道这个MigrateAsync方法,所以我阅读了如何在启动时调用这个方法

https://andrewlock.net/running-async-tasks-on-app-startup-in-asp-net-core-part-1/

但在我读到的任何地方,这种方法都不应该用于生产,因为这些迁移不会在单个事务中执行(没有错误回滚)。

不幸的是,无论环境如何,都没有太多关于如何做到这一点的资源,我找到了这篇文章

https://www.thereformedprogrammer.net/handling-entity-framework-core-database-migrations-in-production-part-2

一种选择可能是调用迁移的控制台应用程序

https://www.thereformedprogrammer.net/handling-entity-framework-core-database-migrations-in-production-part-2/#1b-calling-context-database-migrate-via-a-console-app-or -管理员命令

但我无法理解这种方法的区别,因为它没有解决事务问题?

在开发/生产期间应用迁移的最佳实践是什么?

  • 在自动生成迁移之后,我非常喜欢简单性,dotnet ef database update这项工作不需要我使用其他工具吗?

  • 创建一个控制台应用程序,从迁移生成 .sql 文件,安装 DbUp 并将其用于迁移部分?

c# entity-framework-core dbup

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

使用 CSS 在 flexbox 中创建侧边栏

在我的页面上,我想有一个标题,在此之下,我想在左侧有一个侧边栏,在右侧有一个内容页面。

侧边栏的宽度应为 X(可能为 100 像素),内容页面应包含整个窗口的其余部分。

页

我开始创建这个,但我的侧边栏和内容页面没有全高。即使将高度设置为 100%,也不会填充页面的其余部分。

为什么我必须为侧边栏设置最小和最大宽度而不是宽度?设置width: 100px宽度时返回 70px。

html {
  height: 100%;
}

body {
  height: 100%;
  margin: 0;
  font-family: Ubuntu;
  background: linear-gradient(#b3ffab, #67fffc);
}

#header {
  height: 30px;
  display: flex;
  align-items: center;
  background: linear-gradient(#444444, #333333);
  color: #bbbbbb;
}

#headerContent {
  margin-left: 10px;
}

#page {
  display: flex;
}

#sideBar {
  min-width: 100px;
  max-width: 100px;
  background: red;
}

#content {
  width: 100%;
  background: blue;
}
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Ubuntu" />

<div id="header">
  <div id="headerContent">
    Desktop
  </div> …
Run Code Online (Sandbox Code Playgroud)

html css flexbox

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

为自引用表设置一对多关系

我有一个Project带有非自动生成的 id 字段和一个后继字段的实体。这个继任者是接下来的项目。但也许没有后续项目,所以这可能为空。

@Entity()
export class Project extends BaseEntity {
  @PrimaryColumn({ unique: true })
  public id: string;

  @OneToMany(() => Project, project => project.id, { nullable: true })
  public successorId?: string;
}
Run Code Online (Sandbox Code Playgroud)

通过创建新项目时

public createProject(id: string, successorId?: string): Promise<Project> {
  const project: Project = new Project();
  project.id = id;
  project.successorId = successorId;
  return project.save();
}
Run Code Online (Sandbox Code Playgroud)

有多种情况我必须处理。

  • 传入一个已经存在的 id:

    这不会抛出错误。它只是覆盖现有实体。

  • 传递undefinedsuccessorId

    然后代码工作正常,但它不会创建一个successorIdnull。该列根本不存在于数据库中。

  • idand传递相同的 id successorId(这应该是可能的):

    TypeORM 抛出错误

    类型错误:无法读取未定义的属性“joinColumns”

  • 传入 …

sql postgresql orm typeorm nestjs

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

从没有承载前缀的授权标头中获取访问令牌

我正在为我的 .NET Core 项目使用Microsoft.AspNetCore.Authentication.JwtBearerSystem.IdentityModel.Tokens.Jwt包。

有一些受[Authorize]注释保护的控制器端点必须从请求中获取访问令牌。目前我正在以这种方式在我的控制器方法中获取访问令牌:

string accessTokenWithBearerPrefix = Request.Headers[HeaderNames.Authorization];
string accessTokenWithoutBearerPrefix = accessTokenWithBearerPrefix.Substring("Bearer ".Length);
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更好的“即用型”解决方案,因为使用上面的代码在从承载令牌中获取子字符串时可能仍然会导致错误。

c# adal .net-core

6
推荐指数
2
解决办法
6763
查看次数

如何在 DI 设置期间自动验证 appSettings.json 文件中的配置值?

我向 .NET Core 项目中的 appSettings.json 文件添加了配置。为简单起见,我以数据库设置为例。所以在设置文件中你会有

{
  "Database": {
    "Host": "localhost",
    "Port": 1234,
    "Database": "myDb",
    "Username": "username",
    "Password": "pw",
    "EnablePooling": true
  }
}
Run Code Online (Sandbox Code Playgroud)

在 Startup.cs 文件中配置服务时,我想让这些设置通过依赖注入访问。这个数据模型是

public class DatabaseSettings
{
    public string Host { get; set; }
    public ushort Port { get; set; }
    public string Database { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public bool EnablePooling { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我这样配置

private void SetupSettings(IServiceCollection services)
{
    ServiceProvider …
Run Code Online (Sandbox Code Playgroud)

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

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

如何判断用户是否加入/切换/离开语音频道?

我正在使用 Discord.Net 并观察多个语音频道。如果这些语音通道处于静音状态,由机器人设置(不是通过权限),则该语音通道中的用户也应该被静音。

正如您在此处看到的那样,简单地从语音频道中删除发言权限不会立即影响人们

https://support.discord.com/hc/en-us/community/posts/360052856033-Directly-affect-people-in-channels-on-permission-changes

如果他们离开它,他们应该被取消静音。

所以这个包含所有必需的信息

public sealed class ObservedVoiceChannel
{
    public ulong VoiceChannelId { get; set; }
    public bool IsMuted { get; set; }
    // ... other information go here ...
}
Run Code Online (Sandbox Code Playgroud)

并且我有一个服务保存所有观察到的语音频道

public sealed class ObservedVoiceChannelsCache : Dictionary<ulong, ObservedVoiceChannel>
{
}
Run Code Online (Sandbox Code Playgroud)

由于只有一个UserVoiceStateUpdated事件,我想出了以下代码。

经过一些测试,我认为这段代码对我来说很好用。虽然我知道 if 语句可以通过“或”运算符在可读性方面得到改进,但我会在解决最后一个问题后进行改进。

离开观察到的静音语音频道时,请参阅评论

// 用户离开观察到的静音语音通道

用户不会被机器人取消静音。有时,当加入和离开足够快时,处理程序会抛出异常

服务器响应错误 400:BadRequest

在 Discord.Net.Queue.RequestBucket.SendAsync(RestRequest 请求)
在 Discord.Net.Queue.RequestQueue.SendAsync(RestRequest request) 在 Discord.API.DiscordRestApiClient.SendInternalAsync(String method, String endpoint, RestRequest request) at Discord.API.DiscordRestApiClient.SendJsonAsync(String method, String endpoint, Object payload, BucketIdbucketId, ClientBucketType clientBucket, RequestOptions …

c# discord.net

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

健康检查端点生成的 HealthReport 对象结构冻结 Swagger 文档页面

我为我的 .Net 5 Web API 项目启用了健康检查

public sealed class Startup
{
    public void ConfigureServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddHealthChecks();

        // ...
    }

    public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment webHostEnvironment)
    {
        // ...

        applicationBuilder.UseHealthChecks("/health");

        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,健康端点没有出现在 swagger 文档中。这就是为什么我创建了一个额外的控制器

[ApiController]
[Route("[controller]")]
public sealed class HealthController : ControllerBase
{
    private readonly HealthCheckService _healthCheckService;

    public HealthController(HealthCheckService healthCheckService)
    {
        _healthCheckService = healthCheckService;
    }

    [HttpGet]
    public async Task<ActionResult<HealthReport>> GetHealthIndication()
    {
        HealthReport healthReport = await _healthCheckService.CheckHealthAsync();

        if (healthReport.Status == HealthStatus.Healthy)
        {
            return Ok(healthReport);
        }

        int serviceUnavailableStatusCode = …
Run Code Online (Sandbox Code Playgroud)

c# .net-core asp.net-core-webapi .net-5

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

POSTGRESQL:“utf8”编解码器无法解码位置 36 中的字节 0xfc:起始字节无效

我刚刚从这里下载并安装了 PostgreSQL

https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

我想创建我的第一个服务器,但收到此错误

“utf8”编解码器无法解码位置 36 中的字节 0xfc:起始字节无效

错误

我的 pg 服务自动运行

服务

我缺少什么?

pgadmin-4

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