0

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??

SalShah
  • 43
  • 1
  • 2
  • 8

1 Answers1

2

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)].

gilliduck
  • 2,762
  • 2
  • 16
  • 32