sqlite错误19:'不是零约束失败' - 默认情况下的字符串在.net6/c#8中是否可以无效?

发布于 2025-01-18 11:29:29 字数 1152 浏览 0 评论 0原文

将项目从.NET 5到.NET 6迁移后,我正在遇到这个问题。我将最新版本的SQLite用作数据库。

Visual Studio似乎在假设String默认情况下不再可撤消。每当实体框架试图写入不是主键或外键的字段时,我都会得到错误sqlite错误19:'不是零约束失败。在.NET 5中,我没有遇到此错误。

我还注意到,VS 2022 IntelliSense在每个属性名称下都有一个绿色指示线,引起了此错误。它指出不可拆卸的属性必须在退出构造函数时包含非零值。考虑将属性声明为无效。

代码的示例导致错误:

    [Table("ApplicationUser")]
    public class ApplicaitonUser : IdentityUser
    {
        // Inherited properties https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-6.0
        [Key]
        public new int Id { get; set; } // every other property causes the SQLite 19 error
        
        [PersonalData]
        public string FirstName { get; set; }

        [PersonalData]
        public string MiddleName { get; set; }
        
        [PersonalData]
        public string LastName { get; set; }

        [PersonalData]
        public string PreferredName { get; set; }

        // Address Table Foreign Key relationship navigation property 
        [PersonalData]
        ICollection<UsAddress> UsAddresses { get; set; }


    }

I am having this issue after migrating my project from .NET 5 to .NET 6. I am using the latest version of SQLite as the database.

Visual Studio seems to be assuming that string is no longer nullable by default. Whenever Entity Framework tries to write to a field that is not a primary or foreign key I get the error SQLite Error 19: 'NOT NULL constraint failed. In .NET 5 I was not getting this error.

I also noticed that VS 2022 Intellisense has a green indicator line under each of the property names that cause this error. It states Non-nullable property must contain a non null-value when exiting constructor. Consider declaring the property as nullable.

Example of code the causes the error:

    [Table("ApplicationUser")]
    public class ApplicaitonUser : IdentityUser
    {
        // Inherited properties https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-6.0
        [Key]
        public new int Id { get; set; } // every other property causes the SQLite 19 error
        
        [PersonalData]
        public string FirstName { get; set; }

        [PersonalData]
        public string MiddleName { get; set; }
        
        [PersonalData]
        public string LastName { get; set; }

        [PersonalData]
        public string PreferredName { get; set; }

        // Address Table Foreign Key relationship navigation property 
        [PersonalData]
        ICollection<UsAddress> UsAddresses { get; set; }


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2025-01-25 11:29:29

可无用的意识上下文编号

,在.NET 6/C#8 Microsoft引入了“ 无效的意识上下文”。启用后,此上下文使字符串默认情况下不可删除。与其他类型一样,要无效,就需要无效的指定。这对于测试等非常有用,并且与强烈键入的框架更一致。

This feature can be turned on and off using the enable or disable attribute in the Project file.由于默认情况下禁用该功能,因此也可以将属性删除以禁用其。

noreflable'>

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

nullable 上下文意识上下文:

  • 参考类型的变量t必须用非null,
    并且永远不会分配一个可能为null的值。
  • 参考类型的变量t?可以用null或
    分配的null,但要求对null进行检查
    删除引用。
  • 类型t?的变量m在应用时被视为非零件
    NULL-FORGIVIVER操作员,如m!

不可删除的参考类型t与不可nullable参考类型之间的区别T?由编译器对前面规则的解释强制执行。类型t的变量和类型t?的变量由相同的.NET类型表示。

noflow noreferrer”> nullable参考

=

string notNullable = "Hello";
string? nullable = default;
notNullable = nullable!; // null forgiveness

类型

microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types“ rel https://learn.microsoft.com/en-us/dotnet/csharp/nullable-warnings“ rel =“ nofollow noreferrer”>学习解决可确定可确定警告的技术

更新具有可用参考类型的代码库,以改善无效的诊断警告

Nullable Aware Context

No, in .Net 6/C#8 Microsoft introduced "nullable aware context". When enabled, this context makes strings not-nullable by default. To be nullable then requires a nullable designation, like other types. This is useful for testing, etc... And is more consistent with a strongly typed framework.

This feature can be turned on and off using the <nullable>enable</nullable or <nullable>disable</nullable> attribute in the Project file. Since the feature is disabled by default, the attribute can also be removed to disable it.

Nullable contexts

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

In a nullable aware context:

  • A variable of a reference type T must be initialized with non-null,
    and may never be assigned a value that may be null.
  • A variable of a reference type T? may be initialized with null or
    assigned null, but is required to be checked against null before
    de-referencing.
  • A variable m of type T? is considered to be non-null when you apply
    the null-forgiving operator, as in m!.

The distinctions between a non-nullable reference type T and a nullable reference type T? are enforced by the compiler's interpretation of the preceding rules. A variable of type T and a variable of type T? are represented by the same .NET type.

Nullable reference types

Examples:

string notNullable = "Hello";
string? nullable = default;
notNullable = nullable!; // null forgiveness

Additional information:

Learn techniques to resolve nullable warnings

Update a codebase with nullable reference types to improve null diagnostic warnings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文