Trying to set a non-null string to type 'System.Int32'
Solution 1
Just for anyone else having issues with this. Set a breakpoint in DatabaseContext
and make sure it's connecting to the correct database. Mine was being overwritten from a web.config file I forgot about.
Solution 2
Sounds like your schema and the Entity don't match up, You posted your Code first generated code but that might have changed since the table was created. take a look at the table in SQL Server Manager and double check the data type for that column.
Solution 3
I came across this SO post on the way to solving a similar problem.
I found that my stored procedure had conditional logic that could return either a string or an int for the first column in the result set.
EF seemed to pick up the first bit of result set code it found, which returned a string.
I fixed this by having the sproc return a string either way.
Solution 4
If it's nullable why do you have it marked as required? Remove the required flag and it should behave as expected.
Solution 5
I had a similar problem with a proc import into the EF model and it turned out that the select statement had changed.EF was seeing the result field as a string rather than the int which was defined in the original generated model. We had to explicitly cast the field in the proc to an int and all was happy again.
Alexander Forbes-Reed
Updated on March 15, 2020Comments
-
Alexander Forbes-Reed about 4 years
Entity Framework is throwing this exception:
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'.
It's throwing on this line:
// Validate uniqueness or email and username var user = sqlStorage.BranchIdentities.FirstOrDefault(i => i.Username.ToLower() == viewModel.Username.ToLower());
The exception only throws when there is an entity that matches the query. If there are no matches, the exception isn't thrown.
My BranchIdentity model:
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; } } }
And my schema (taken from the sql database) - auto-generated using code-first migrations:
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]) );
I have tried making
PasswordIterations
nullable, but to no avail. -
Alexander Forbes-Reed over 9 yearsI set it to nullable to try and fix the issue. Originally it wasn't nullable - I've updated my example to show this.
-
Alexander Forbes-Reed over 9 yearsThe schema I posted was from the database, rather the CodeFirst migration. I'll update my question to clarify.
-
Grax32 over 9 yearsI put your posted schema and EF model into my solution and it worked great. Then I changed the database type of PasswordIterations to a varchar and got the precise exception you show above. This answer is almost certainly the right answer, so I would double and triple check your connection strings. Also, running SQL Server Profiler would be a great step. If it shows no activity, then your project isn't connecting to the db you think it is.
-
Alexander Forbes-Reed over 9 yearsYou were right. It was connecting to some random database.
-
JoshYates1980 almost 9 yearsThanks. I had public int foo instead of public string foo. geez.