the column mapping in the dbcontext is wrong, or when you ported to Sqlserver you made the column types different. for us to fix, we would need the sql server table definition, and the table definition in the dbcontext.
Connexion to database SQL SERVER (2019)
Hi,
I've an Blazor Wasm application.
Until now I worked with a local database that I've created by migration.
"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\...\\source\\repos\\DATASERVEUR\\Developers.mdf;Integrated Security=True;Connect Timeout=30"
With this connection all things is ok
Now I want to connect my application with the data base in sql server (2019)
Data Source=servername;Initial Catalog=Developers;Integrated Security=True;
the table dbo.Developers was created by import of the flat file (csv)
When I run my application I've a message :
InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32'.
The get action where I've this message :
[HttpGet]
public async Task<IActionResult> Get()
{
var devs = await _context.Developers.ToListAsync();
return Ok(devs);
}
Have you an idea how I can solve this problem?
thanks in advance
8 answers
Sort by: Most helpful
-
-
Naomi 7,366 Reputation points
2022-04-22T15:43:35.057+00:00 I have a vague recollection I ran into this problem when one of the columns was using either tinyint or smallint type in the database and the model was using byte. I forgot the resolution, unfortunately.
-
Naomi 7,366 Reputation points
2022-04-22T16:31:04.253+00:00 What happens if you remove this for now
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int Calcul
{
get
{
if (DateSoldePr.Year < 2000)
{
return 0;
}
if (DateSoldePr.Year > 2000)
{
return (int)Math.Floor((DateTime.Today - DateSoldePr).TotalDays);
}
else
{
return 0;
}
}
}? Could it be due to nullability in that column?
-
sblb 1,171 Reputation points
2022-04-22T16:16:05.483+00:00 Table definition in the dbcontext.
// [NotMapped] public partial class Developer { public int Id { get; set; } // [Required] // [StringLength(6)] // [Display(Name = "Numéro ECR")] public string ECR { get; set; } public DateTime DateCrea { get; set; } public string Atelier { get; set; } public string Nom { get; set; } public string PNC { get; set; } public string DesignationC { get; set; } public string AmdtC { get; set; } public string IndiceC { get; set; } public string PNP { get; set; } public string Produit { get; set; } public string AmdtP { get; set; } public string IndiceP { get; set; } public DateTime DateP { get; set; } // [Required] public string Description { get; set; } public string Analyse { get; set; } public string BlocT { get; set; } public string BlocM { get; set; } public string FEE { get; set; } public string Statut { get; set; } public string CodePrio { get; set; } public DateTime DateSoldePr { get; set; } public DateTime DateSuivi { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public int Calcul { get { if (DateSoldePr.Year < 2000) { return 0; } if (DateSoldePr.Year > 2000) { return (int)Math.Floor((DateTime.Today - DateSoldePr).TotalDays); } else { return 0; } } } public int RAF { get; set; } public string CommentairePrio { get; set; } public string DTbem { get; set; } public string DTbee { get; set; } public string DTrd { get; set; } public string DImi { get; set; } public string BlSAQ { get; set; } public string ECO { get; set; } public DateTime DateSolde { get; set; } public string Commentaires { get; set; } }
sql server table definition
CREATE TABLE [dbo].[Developers]( [Id] [int] NOT NULL, [ECR] [nvarchar](6) NOT NULL, [DateCrea] [datetime] NOT NULL, [Atelier] [nvarchar](50) NOT NULL, [Nom] [nvarchar](50) NOT NULL, [PNC] [nvarchar](50) NOT NULL, [DesignationC] [nvarchar](50) NOT NULL, [AmdtC] [nvarchar](50) NOT NULL, [IndiceC] [nvarchar](50) NOT NULL, [PNP] [nvarchar](50) NULL, [Produit] [nvarchar](50) NOT NULL, [AmdtP] [nvarchar](50) NOT NULL, [IndiceP] [nvarchar](50) NOT NULL, [DateP] [datetime] NOT NULL, [Description] [nvarchar](max) NOT NULL, [Analyse] [nvarchar](max) NOT NULL, [BlocT] [int] NOT NULL, [BlocM] [int] NOT NULL, [FEE] [nvarchar](1) NULL, [Statut] [nvarchar](10) NULL, [CodePrio] [nvarchar](10) NULL, [DateSoldePr] [datetime] NULL, [DateSuivi] [datetime] NULL, [RAF] [int] NULL, [CommentairePrio] [nvarchar](max) NULL, [DTbem] [nvarchar](10) NULL, [Dtbee] [nvarchar](10) NULL, [DTrd] [nvarchar](10) NULL, [DImi] [nvarchar](10) NULL, [BlSAQ] [nvarchar](10) NULL, [ECO] [nvarchar](10) NULL, [DateSolde] [datetime] NULL, [Commentaires] [nvarchar](max) NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO