I need an exampel for a MVVM with foreign key

simsenVejle

Well-known member
Joined
Feb 14, 2021
Messages
46
Programming Experience
Beginner
I have 2 models
CategoryModel
---CategoryId
---CategoryName
---ProjectId (this i foreign key)

ProjectModel
---ProjectId
---ProjectName

Those I don't have problems in making.

But now it gets tricky to me. What to do in the VM and also in the View.

In the View I have a ListView where the columns are
---CategoryId
---CategoryName
---ProjectName

Right now I have made a 4. entry in the Category that says ProjectName (and there I have made a join in the stored procedure to the 2 tables. But I think that is very bad. Because the ProjectName shouldn't be in the Category Model.

I have looked on google for the hole day and can't find a tutorial that shows me, how to join the 2 models in the viewmodel and if that isn't the right way how the to make the ListView.

I'm properbly using the wrong sentence in google search.

I would be glad for any kind of help.

Best regards
Simsen :)
 
Solution
Unfortunately, your basic relational database setup is wrong. You need the following schema:

Category:
Id
Name

Project:
Id
Name

ProjectCategory:
Id
ProjectId
CategoryId
Unfortunately, your basic relational database setup is wrong. You need the following schema:

Category:
Id
Name

Project:
Id
Name

ProjectCategory:
Id
ProjectId
CategoryId
 
Last edited:
Solution
Unfortunately, your basic relational database setup is wrong. You need the following schema:

Category:
Id
Name

Project:
Id
Name

ProjectCategory:
Id
ProjectId
CategoryId
Hi,
Thank you for your answer.

Your db layout is that if there is a many to many relationship. That is mine not. I can only have one project to one category.


If that isn't the case I can't see, what to do in the VM.

In the view I want to show CategoryId, CategoryName and ProjectName (not Id).

So if I took your advise I'm confused what to do in the model and most important how to make it so, I can see the 3 entries from Model in the ViewModel?

Best regards
Simsen :)
 
So if you have 100 projects, you will have exactly 100 categories? If that's the case, then simply layout your records to be:

Project
Id
Name
Category

and be done with it.
 
So if you have 100 projects, you will have exactly 100 categories? If that's the case, then simply layout your records to be:

Project
Id
Name
Category

and be done with it.
I can have a number of categories that hasn't any project. And I can have projects that hasn't been assigned to any Category
 
Okay, so what's wrong the M:N relationship showed in post #3? There's no requirement that M > 1 or N > 1.
 
Back
Top Bottom