简体   繁体   中英

How call tables without primary key with entity framework core

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM