gpt4 book ai didi

c# - 尝试将非空字符串设置为类型 'System.Int32'

转载 作者:可可西里 更新时间:2023-11-01 08:35:03 26 4
gpt4 key购买 nike

Entity Framework 抛出这个异常:

The 'PasswordIterations' property on 'BranchIdentity' could not be set to a 'System.String' value. You must set this property to a non-null value of type '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,但无济于事。

最佳答案

听起来您的架构和实体不匹配,您首先发布了代码生成的代码,但自创建表以来可能已经更改。查看 SQL Server 管理器中的表并仔细检查该列的数据类型。

关于c# - 尝试将非空字符串设置为类型 'System.Int32',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com