Home > Software design >  asp.net core 6 EF scaffolding with Oracle database
asp.net core 6 EF scaffolding with Oracle database

Time:12-20

I am creating Asp.net core 6 web api with oracle 19 as backend. i am using DB first approach and create model object from DB table. i used the below syntax,

 scaffold-dbcontext "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=TestDB.Test.com)(PORT=1717))(CONNECT_DATA=(SERVICE_NAME=TestDB)));User Id=usename;Password=password;Persist Security Info=True;Connection Lifetime=100000;" Oracle.EntityFrameworkCore -o ModelFolder -f  

Note : i removed the secured data and gave dummy data on the above code. I was able to generate the model from my backend. but having some issues with DB type. i am trying to call the EF query to get the data and facing cast issue. one of the table column is datatype is Number(3) and model got created as Byte?. when it try to bind the entity result, it gives me specified cast is not valid. its expecting Int. am not sure how to solve the issue. i should not be modified the auto generated model property to int. not sure is this ongoing issue with asp.net core with oracle. requesting any suggestion to solve this error.

CodePudding user response:

I am trying to call the EF query to get the data and facing cast issue. one of the table column is datatype is Number(3) and model got created as Byte?. when it try to bind the entity result, it gives me "specified cast is not valid"

It would be nicer, if you could add your database entity schema defination and the asp.net core POCO Class. Thus, based on the error, you should consider following data types which usually treated as inconsistent within Oracle to Asp.net core entity scaffolding while generate automatically.

Please Cheeck Following data type:

byte? should be long in asp.net core,

short? should be int

bool? should be changed to int if it contains number like 0 or 1 other than oracle number should be deal with decimal

Note: When we encounter "specified cast is not valid" it convery the meaning, POCO Class and data entity model error mostly related to defination of asp.net core POCO class which doesn't matached with the database entity defination. You could follow this official Chart for reference:

enter image description here

enter image description here

More details can be founded at official document here

  • Related