C
C#4w 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
sibber4w ago
why does a book id have a storefront
sibber
sibber4w 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
eliOP4w 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
sibber4w ago
i dont see why you dont just use another id column or something
eli
eliOP4w ago
That's what I ended up doing
eli
eliOP4w ago
No description
eli
eliOP4w 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
eliOP4w 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