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

6
  • related? stackoverflow.com/questions/41349080/… Commented Jul 9, 2019 at 12:14
  • Sorry, This address doesn't work in my case! Commented Jul 9, 2019 at 12:24
  • you have decorate class property as [Key] Commented Jul 9, 2019 at 12:48
  • It doesn't bring anything from database Commented Jul 9, 2019 at 12:58
  • What version of Ef.Core are you running? Commented Jul 9, 2019 at 13:18

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.