0
votes

I am doing an asp.net core api with odata. My tables in database has no primary key and I do not have permission to change the database. I use below command to call my database:

"dotnet ef dbcontext scaffold "Data Source = 192.168.11.1\sql2016;Initial Catalog=SeeMiddle;persist security info=True;user id=Iaas;password=Iaas123!@#" Microsoft.EntityframeworkCore.SqlServer -d -c SeeMiddleContext -o Models\Entities --force"

and I encounter below errors for every tables:

Unable to identify the primary key for table 'cmn.ReshteNerkhItem'.

Unable to generate entity type for table 'cmn.ReshteNerkhItem'.

how can I work with a database that has any primary key in asp.net core??

1
Sorry, This address doesn't work in my case!SalShah
you have decorate class property as [Key]Jasmin Solanki
It doesn't bring anything from databaseSalShah
What version of Ef.Core are you running?Alexey Andrushkevich

1 Answers

1
votes

Short answer, you don't. Entity Framework requires a key. Good news is you can effectively spoof a key if your table doesn't have one. If your table has an implicit key that is distinct then just decorate that property with [Key] and you'll be good. The key thing is that it has to be a distinct unique value. If you do not have a singular column that does that then you'll need to start using columns together to make a composite key ([Key, Column(Order = 0)].