C
C#13mo ago
Melnar

❔ 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
TravestyOfCode
TravestyOfCode13mo ago
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}.
Melnar
Melnar13mo ago
Different users can create the same cateogry or transaction
TravestyOfCode
TravestyOfCode13mo ago
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))
Accord
Accord13mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts