C
C#•2y ago
BigggMoustache

Scaffolding Error - Requires a primary key to be defined

Here's the class it's referring to
public class ImageUris
{
[JsonPropertyName("small")]
public string Small { get; set; }

[JsonPropertyName("normal")]
public string Normal { get; set; }

[JsonPropertyName("large")]
public string Large { get; set; }

[JsonPropertyName("png")]
public string Png { get; set; }

[JsonPropertyName("art_crop")]
public string ArtCrop { get; set; }

[JsonPropertyName("border_crop")]
public string BorderCrop { get; set; }
}
public class ImageUris
{
[JsonPropertyName("small")]
public string Small { get; set; }

[JsonPropertyName("normal")]
public string Normal { get; set; }

[JsonPropertyName("large")]
public string Large { get; set; }

[JsonPropertyName("png")]
public string Png { get; set; }

[JsonPropertyName("art_crop")]
public string ArtCrop { get; set; }

[JsonPropertyName("border_crop")]
public string BorderCrop { get; set; }
}
I'll be honest I've read the key info in the error link and don't exactly understand what all of this means. I don't quite understand how keys work either.
35 Replies
FroH.LVT
FroH.LVT•2y ago
Maybe you need one of those: 1. Property name ID 2. Add attribute called [Key] to one of your current property 3. Define your Key on ModelCreating 4. Tell EF that your entity doesn't have key using .HasNoKey()
BigggMoustache
BigggMoustache•2y ago
Yeah I get that much, I guess I should ask what a key does. I don't really understand this despite trying to read on the docs so I have a Card { ImageUris ImageUris { get; set; } and then ImageUris { ... }
mikepattyn
mikepattyn•2y ago
A key is the column/property of your row/object by which that object or row will be identified
BigggMoustache
BigggMoustache•2y ago
okie doke
mikepattyn
mikepattyn•2y ago
You can work without tho as says the description you posted
BigggMoustache
BigggMoustache•2y ago
yeah I'm just not understanding tbh
mikepattyn
mikepattyn•2y ago
You know SQL right?
BigggMoustache
BigggMoustache•2y ago
I mean I vaguely understand, but in practice of what I'm trying to do vaguely, I've done a few tutorials
mikepattyn
mikepattyn•2y ago
Okay,
BigggMoustache
BigggMoustache•2y ago
Can I just ask a couple dumb questions particular to what I'm doing? Or questions I imagine will help me understand? lol
mikepattyn
mikepattyn•2y ago
sure sure, ask away
BigggMoustache
BigggMoustache•2y ago
What Could I use as a primary key for the imagesUris?
FroH.LVT
FroH.LVT•2y ago
@@
mikepattyn
mikepattyn•2y ago
public int Id {get; set;} 🙂
BigggMoustache
BigggMoustache•2y ago
the json doesn't serve an ID for the images
FroH.LVT
FroH.LVT•2y ago
u mean you don't want user to see your ID in Json?
BigggMoustache
BigggMoustache•2y ago
again, I'm sorry lol. I am a brick.
mikepattyn
mikepattyn•2y ago
no no he dont wna see id he dno what to do with id
BigggMoustache
BigggMoustache•2y ago
No no, I just wholly don't understand this concept
mikepattyn
mikepattyn•2y ago
where do you get the json?
BigggMoustache
BigggMoustache•2y ago
from an api, I'm trying to make a magic the card game deck building app figure that'd be a fun practical way to learn working SQL
mikepattyn
mikepattyn•2y ago
cool it is but if you dont get the [Key] concept, i wouldnt continue on until i do
BigggMoustache
BigggMoustache•2y ago
So next question I guess. I can take any property in the ImageUris make it the PK, and the Card object will know to associate that specific set of ImageUris with it? I get the idea, I just don't get how to use it I just have to slap a PK on a property, and it'll keep track of which card references which images?
mikepattyn
mikepattyn•2y ago
no not just any property
BigggMoustache
BigggMoustache•2y ago
lol okay
mikepattyn
mikepattyn•2y ago
in sql database, records are indexed. An integer is the best to do this You should add public int to your urls model
BigggMoustache
BigggMoustache•2y ago
the image urls will be unique so I figured that'd be good lol
mikepattyn
mikepattyn•2y ago
and make the Id column in sql to PRIMARY AUTO INCREMENT Well it can work without PK like you say, the urls are unique you just have to configure the entity in the OnModelCreating..
BigggMoustache
BigggMoustache•2y ago
I'm all ears bud. I'll google PRIMARY AUTO INCREMENT too when we're done Yeah I haven't done annnything like that only automated stuff and some sql functions in pgadmin / SSMS I did see some stuff like that in docs so I'll take a second look. I really didn't think it'd be this difficult lol. Every step of this programming journey has felt like climbing a mountain to find another mountain xD
mikepattyn
mikepattyn•2y ago
Thts the job lol But why dont you take a udemy course ?
BigggMoustache
BigggMoustache•2y ago
Well I've done a few MVC tutorials, one was an entire course So I was hoping I'd be able to get it off the ground, but turns out nope
mikepattyn
mikepattyn•2y ago
Sure you will
BigggMoustache
BigggMoustache•2y ago
Should I take an EFCore specific class
mikepattyn
mikepattyn•2y ago
I can screenshare and setup an example real quick if u want
BigggMoustache
BigggMoustache•2y ago
sure that'd be really cool.