❔ Entity relationship Help
I am getting started on a project using .net mvc identity and I am kinda new at this
Here is how the relationship is between entities:
User inherits from Identity<int>
The user can create a category for a transaction
The user can make multiple transactions in the same category
The transaction is an expense or income
Transactions of different type cannot be under the same category
Now i am not sure what each class must have
Should user have ICollection<Category> or ublic ICollection<Transaction> as well?
What about Category
Should it have ICollection<Transaction> and User User as a foreign key?
What about Transaction
Should it have Category Category and User User as foreign keys?
I am unsure how to define the relationship between these 3 entities
4 Replies
Do you need to know which user created the Category and Transaction? If so, you'll need your UserId on the Category/Transaction table. Because a transaction can have a Category, you'll need a CategoryId on the Transaction table.
Adding the specific table type on another table , like
public Category Category { get; set;}
allows you to navigate to the related table from the table. So if you added Category to the Transaction table, you would be able to access the related category information like: transaction.Category.{categoryProperties}
.Different users can create the same cateogry or transaction
So you probably want to keep track of the UserId on the Category and Transaction table.
I don't tend to put navigation properties on my User class, but not sure if that's normal. I don't typically query my user table and just filter on my other tables for the specific UserId. Like
_dbContext.Categories.Where(u => u.UserId.Equals(request.UserId))
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.