實體框架是拋出此異常:試圖設置一個非空字符串類型「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
空的,但無濟於事。
你的'PasswordIterations'被定義爲'Nullable'。所以顯然你不能使用字符串。 –
Rahul
2014-10-17 22:14:39
事情是,我沒有在任何地方使用字符串。它總是一個整數。 – 2014-10-17 22:15:33
我懷疑這是拋出異常的那條線。你確定? – Rahul 2014-10-17 22:16:35