sqlite错误19:'不是零约束失败' - 默认情况下的字符串在.net6/c#8中是否可以无效?
将项目从.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可无用的意识上下文编号
,在.NET 6/C#8 Microsoft引入了“ 无效的意识上下文”。启用后,此上下文使字符串默认情况下不可删除。与其他类型一样,要无效,就需要无效的指定。这对于测试等非常有用,并且与强烈键入的框架更一致。
This feature can be turned on and off using theenable or
disable
attribute in the Project file.由于默认情况下禁用该功能,因此也可以将属性删除以禁用其。noreflable'>
nullable 上下文意识上下文:
t
必须用非null,并且永远不会分配一个可能为null的值。
t?
可以用null或分配的null,但要求对null进行检查
删除引用。
t?
的变量m
在应用时被视为非零件NULL-FORGIVIVER操作员,如
m!
。不可删除的参考类型
t
与不可nullable参考类型之间的区别T?
由编译器对前面规则的解释强制执行。类型t
的变量和类型t?
的变量由相同的.NET类型表示。noflow noreferrer”> nullable参考
=
类型
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
In a nullable aware context:
T
must be initialized with non-null,and may never be assigned a value that may be null.
T?
may be initialized with null orassigned null, but is required to be checked against null before
de-referencing.
m
of typeT?
is considered to be non-null when you applythe null-forgiving operator, as in
m!
.The distinctions between a non-nullable reference type
T
and a nullable reference typeT?
are enforced by the compiler's interpretation of the preceding rules. A variable of typeT
and a variable of typeT?
are represented by the same .NET type.Nullable reference types
Examples:
Additional information:
Learn techniques to resolve nullable warnings
Update a codebase with nullable reference types to improve null diagnostic warnings