我環顧了各種相關問題,但似乎找不到一個可以幫助我解決這個問題的問題。
我使用 EF Core Power Tools 來設定我的 DbContext;一個具體物體如下:
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace FIS2DataTables.Tables
{
[Table("Settings", Schema = "static")]
[Index(nameof(Setting), IsUnique = true)]
public partial class Settings
{
[Key]
[Column("id")]
public int Id { get; set; }
[Required]
[Column("setting")]
[StringLength(50)]
public string Setting { get; set; }
[Required]
[Column("value")]
[StringLength(200)]
public string Value { get; set; }
[Column("intValue")]
public int? IntValue { get; set; }
[Column("bitValue")]
public bool? BitValue { get; set; }
}
}
現在我有兩個不同的檔案參考了這個特定的物體。這是這些檔案之一(簡化):
using FIS2DataTables.Context;
using FIS2DataTools.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace FIS2DataTools.Services
{
public class SettingsService : ISettingsService
{
private readonly IConfiguration Configuration;
private readonly IDbContextFactory<FIS2TableContext> ContextFactory;
public SettingsService(IDbContextFactory<FIS2TableContext> contextFactory, IConfiguration configuration)
{
ContextFactory = contextFactory;
Configuration = configuration;
}
private T GetConfigSetting<T>(string toFind)
{
return Configuration.GetValue<T>(toFind);
}
private string GetDbSetting(string toFind)
{
using (FIS2TableContext context = ContextFactory.CreateDbContext())
{
return context.Settings.FirstOrDefault(x => x.Setting == toFind).Value;
}
}
private async Task<string> GetDbSettingAsync(string toFind)
{
using (FIS2TableContext context = ContextFactory.CreateDbContext())
{
return (await context.Settings.FirstOrDefaultAsync(x => x.Setting == toFind)).Value;
}
}
public string GetDomainName()
{
return GetDbSetting("DomainName");
}
public string GetPhotosPath()
{
return GetDbSetting("PhotosPath");
}
public async Task<string> GetPhotosPathAsync()
{
return await GetDbSettingAsync("PhotosPath");
}
public string GetStoragePath()
{
return GetConfigSetting<string>("StoragePaths:Local");
}
}
}
有問題的方法是GetDbSetting和它的異步對應方法。在這兩種方法中,此塊:context.Settings.FirstOrDefault(x => x.Setting == toFind)標記為:
取消參考可能為空的參考
我的挖掘基本上說這只是編譯器的說法:
這可能,也許,在某些時候是空的......我不確定
但是......我在解決方案的其他地方有完全相同的代碼(我正在將它從專案 A 遷移到 B 的中途),并且在其他位置它沒有標記它。
First and foremost, I'm surprised by the inconsistency of it; but moreover, I'm wondering what the best course of action to avoid this is?
EDIT: now that I've asked the question; I've realised the instance that gets flagged, is the one where I'm referencing it in a seperate project - it's not as simple as it being that tying the analyser in knots?
For reference, the the other file (the one where it doesn't get flagged) is as follows:
using FIS2DataLibrary.Data.Interfaces;
using FIS2DataLibrary.Helpers;
using FIS2DataLibrary.Tables;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.Linq;
using System.Threading.Tasks;
namespace FIS2DataLibrary.Data.Services
{
public class SettingsService : ISettingsService
{
private readonly IConfiguration Configuration;
private readonly IDbContextFactory<FIS2_TableContext> ContextFactory;
public SettingsService(IDbContextFactory<FIS2_TableContext> contextFactory, IConfiguration configuration)
{
ContextFactory = contextFactory;
Configuration = configuration;
}
private T GetConfigSetting<T>(string toFind)
{
return Configuration.GetValue<T>(toFind);
}
private string GetDbSetting(string toFind)
{
using (FIS2_TableContext context = ContextFactory.CreateDbContext())
{
return context.Settings.FirstOrDefault(x => x.Setting == toFind).Value;
}
}
private async Task<string> GetDbSettingAsync(string toFind)
{
using (FIS2_TableContext context = ContextFactory.CreateDbContext())
{
return (await context.Settings.FirstOrDefaultAsync(x => x.Setting == toFind)).Value;
}
}
public string GetDomainName()
{
return GetDbSetting("DomainName");
}
public async Task<MailServerConnectionSettings> GetMailServerConnectionSettingsAsync()
{
return new MailServerConnectionSettings(await GetDbSettingAsync("MailHost"), await GetDbSettingAsync("MailUser"), await GetDbSettingAsync("MailPass"));
}
public string GetPhotosPath()
{
return GetDbSetting("PhotosPath");
}
public async Task<string> GetPhotosPathAsync()
{
return await GetDbSettingAsync("PhotosPath");
}
public string GetStoragePath()
{
return GetConfigSetting<string>("StoragePaths:Local");
}
}
}
The ONLY difference between the two is that the one that isn't flagged exists in the same solution as its version of the DbContext; whereas the version that gets flagged references the DbContext from a seperate project.
uj5u.com熱心網友回復:
由于唯一的區別是代碼位于不同的專案中 - 那么帶有警告的專案應該啟用可空參考型別。檢查Nullable相應 .csproj 中的 xml 元素。
您可以禁用可空參考型別或(我認為更好)處理潛在的可空性(例如,First 如果您確定查詢應該始終回傳某些內容,則通過“洗掉”它)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/417749.html
標籤:
