8

實體框架是拋出此異常:試圖設置一個非空字符串類型「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空的,但無濟於事。

+0

你的'PasswordIterations'被定義爲'Nullable '。所以顯然你不能使用字符串。 – Rahul 2014-10-17 22:14:39

+0

事情是,我沒有在任何地方使用字符串。它總是一個整數。 – 2014-10-17 22:15:33

+0

我懷疑這是拋出異常的那條線。你確定? – Rahul 2014-10-17 22:16:35

回答

7

只爲其他人有此問題。在DatabaseContext中設置斷點並確保它連接到正確的數據庫。我被我忘記的web.config文件覆蓋了。

+1

謝謝。我有公共int foo而不是公共字符串foo。天啊。 – JoshYates1980 2015-06-11 16:38:21

2

如果它是可以爲空的,爲什麼你需要標記它?刪除所需的標誌,它應該按預期行事。

+0

我將其設置爲空來嘗試解決問題。最初它不是可以空的 - 我已經更新了我的例子來顯示它。 – 2014-10-18 11:16:53

8

聽起來像你的模式和實體不匹配,你發佈了你的代碼第一次生成的代碼,但自表創建以來可能已經改變。請查看SQL Server管理器中的表,然後仔細檢查該列的數據類型。

+0

我發佈的模式來自數據庫,而不是CodeFirst遷移。我會更新我的問題以澄清。 – 2014-10-18 15:06:54

+1

我把你的發佈架構和EF模型放到我的解決方案中,它效果很好。然後我將PasswordIterations的數據庫類型更改爲一個varchar,並得到了上面顯示的精確異常。這個答案几乎肯定是正確的答案,所以我會雙倍和三倍檢查你的連接字符串。另外,運行SQL Server Profiler將是一個很好的步驟。如果它沒有顯示任何活動,那麼你的項目沒有連接到你認爲它的db。 – Grax 2014-10-23 18:44:58

+0

你說得對。它連接到一些隨機數據庫。 – 2014-10-27 15:58:01

1

我有一個proc導入到EF模型的類似問題,並且事實證明select語句已經改變.EF將結果字段看作字符串而不是在原始生成的模型中定義的int。我們必須明確地將proc中的字段賦給int,並且所有的都很快樂。

3

我在解決類似問題的路上遇到了這個SO帖子。

我發現我的存儲過程有條件邏輯,可以返回結果集中第一列的字符串或int。

EF似乎拿起找到的結果集代碼的第一位,它返回一個字符串。

我通過讓sproc返回一個字符串來解決這個問題。

0

與View有相同的問題。顯式轉換爲視圖腳本中有問題的列的nvarchar,並修復了模型的更新

0

在將數據類型從string更改爲int後出現此類錯誤的可能性。

變化

public string foo { get; set; } 

之前變化

public int foo { get; set; } 

這種變化之前之後,string將允許插入null值到表中。改變之後,那些舊的零位可能成爲拋出這種錯誤的原因。

在表號替換爲空值(例如:0)

0

問題是解決了,但它可能是有用的爲別人:

確保你寫入數據庫的正確連接,並且可以通過以下方式使c#生成連接字符串:
添加一個新項目 - >數據 - > ADO.Net實體數據模型 - >代碼從數據庫開始--->新建連接--->並指定您的參數和測試連接 - >好 - >好 - >完成

open web.co nfig,你會發現你的連接字符串


還檢查是否存在於數據庫中的空列中實現它的類,如下所示: 爲前:公衆詮釋? WorkedYears {get;組; }
裁判:EFCode First Property Null problem

1
在我真正的數據庫

我場爲null能夠INT,但在我的代碼模型我的領域很簡單INT。我把它改爲int? (可爲空),現在它工作得很好。

相關問題