Trying to set a non-null string to type #39;System.Int32#39;(正在尝试将非空字符串设置为System.Int32#39;类型)
本文介绍了正在尝试将非空字符串设置为System.Int32';类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实体框架引发此异常:
"BranchIdentity""的""PasswordIterations""属性无法设置为""System.String值""。""必须将此属性设置为‘System.Int32’类型的非空值。
它在这条线上抛出:
// Validate uniqueness or email and username
var user = sqlStorage.BranchIdentities.FirstOrDefault(i => i.Username.ToLower() == viewModel.Username.ToLower());
仅当存在与查询匹配的实体时才引发异常。如果没有匹配项,则不会引发异常。
我的BranchIdentity模型:
namespace Branch.Models.Sql
{
public class BranchIdentity
{
[Key]
public int Id { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string PasswordHash { get; set; }
[Required]
public string PasswordSalt { get; set; }
[Required]
public int PasswordIterations { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string FullName { get; set; }
public virtual ICollection<BranchIdentitySession> BranchIdentitySessions { get; set; }
public virtual BranchRole BranchRole { get; set; }
public virtual GamerIdentity GamerIdentity { get; set; }
}
}
和我的架构(取自SQL数据库)-使用代码优先迁移自动生成:
CREATE TABLE [dbo].[BranchIdentities] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Username] NVARCHAR (MAX) NOT NULL,
[PasswordHash] NVARCHAR (MAX) NOT NULL,
[PasswordSalt] NVARCHAR (MAX) NOT NULL,
[PasswordIterations] INT NOT NULL,
[Email] NVARCHAR (MAX) NOT NULL,
[BranchRole_Id] INT NULL,
[GamerIdentity_Id] INT NULL,
[FullName] NVARCHAR (MAX) DEFAULT ('') NOT NULL,
CONSTRAINT [PK_dbo.BranchIdentities] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_dbo.BranchIdentities_dbo.BranchRoles_BranchRole_Id] FOREIGN KEY ([BranchRole_Id]) REFERENCES [dbo].[BranchRoles] ([Id]),
CONSTRAINT [FK_dbo.BranchIdentities_dbo.GamerIdentities_GamerIdentity_Id] FOREIGN KEY ([GamerIdentity_Id]) REFERENCES [dbo].[GamerIdentities] ([Id])
);
我已尝试使PasswordIterations
可为Null,但无济于事。
推荐答案
仅供有此问题的其他人使用。在DatabaseContext
中设置断点,并确保它连接到正确的数据库。我的文件被我忘记的一个web.config文件覆盖了。
这篇关于正在尝试将非空字符串设置为System.Int32';类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:正在尝试将非空字符串设置为System.Int32';类型


猜你喜欢
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01