C
C#2mo ago
eli

Possible to use a class as composite key in entity framework?

class Book
{
[Key] public required BookIdentifier Id { get; init; }
}

[Owned]
class BookIdentifier
{
public required Storefront Storefront { get; init; }
public required string ListingId { get; init; }
}
class Book
{
[Key] public required BookIdentifier Id { get; init; }
}

[Owned]
class BookIdentifier
{
public required Storefront Storefront { get; init; }
public required string ListingId { get; init; }
}
Is there any way to do something like this?
8 Replies
sibber
sibber2mo ago
why does a book id have a storefront
sibber
sibber2mo ago
if you want strongly typed ids you can use something like vogen
GitHub
GitHub - SteveDunn/Vogen: A semi-opinionated library which is a sou...
A semi-opinionated library which is a source generator and a code analyser. It Source generates Value Objects - SteveDunn/Vogen
eli
eliOP2mo ago
Just an example, in my actual usecase this makes a lot more sense My app lets people browse products from multiple different storefronts each storefront has it's own Id system, so obv if I just use that I can get overlap between Ids, so I want composite
sibber
sibber2mo ago
i dont see why you dont just use another id column or something
eli
eliOP2mo ago
That's what I ended up doing
eli
eliOP2mo ago
No description
eli
eliOP2mo ago
Literally the only reason I wanted to do this is cause my productidentifier class knows how to get the URL for that product, but it's honestly probably best to separate that stuff
No description
eli
eliOP2mo ago
and also passing at least 2 params to every function related to querying a product is annoying
Want results from more Discord servers?
Add your server