❔ Entity Framework. Rental list
I have a class for books and I have a class of customers. I also have a class for loans.
The books should be possible to be loaned by other customers as well but only after the loaner has given it back and the loaner can choose themselves how many days they want to loan the book, a maximum of a week though.
The loans class should be a junction table, I assume this is a many-to-many relation?
I can figure out the relation-stuff in EF but I'm unsure how to check the timespan between date1 and date2. It's of course of type DateTime. It seems like I can use calculus to calculate the datetimes but how would this look like with either Linq or with a lambda expression?
8 Replies
add a getter for that time span in your class, and just
=> date2 - date1;
calculate the difference there
you're right, you need a junction table with (loan id, person id, book id)
(if you keep the loan history, if you don't, then you don't need the loan id)
the simplest way to set this up would be to make a class Loan
with those three and set up the relations using the fluent builder
then add loan navigation properties as needed to people or books
setting up the relations should be pretty trivial
look up the docs and go through the relevant pageRelationships - EF Core
How to configure relationships between entity types when using Entity Framework Core
just read through this
I recommend you understand the ideas thoroughly
Thank you!
Not sure what you mean with the getter though. I know you mean using the property in get; set; but not sure what => date2 - date1 means.
If you're storing
LoanedOn
and RepaidOn
, you can calculate LoanDuration
using RepaidOn
and LoanedOn
. Traditionally you would use a method to calculate LoanDuration
like GetLoanDuration()
but C# allows you to use 'getters' which are actually methods but you use them like properties (technically they are properties)
if you have a variable loan
, you can access loan.LoanDuration
to get the duration.
This way you don't have to store LoanDuration
separately in a column which would lead to data redundancy (not a good thing—it can lose sync and cause weird bugs)Thank you ringsig! Doesn't that Loan object have to have an id?
Or am I misinterpret something? 🙄
But I do get your timespan property!
Is there also a difference between saying:
LoanDuration => RepaidOn - LoanedOn;
LoanDuration = RepaidOn - LoadedOn:
the second one is an initializer that's run before the constructor
they just showed the relevant bit of the class
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.