✅ EF Core navigational properties
This works fine (no navigational property to Business):
This causes an error:
Initialization code:
How can I make this work while also keeping Address' nav property to Business?
Please @ me so I respond immediately
27 Replies
I assume
is supposed to be
?
Also, don't use
.AddAsync()
, it's not an inherently asynchronous operation.I just solved the problem
Old code:
New code:
I initialize Address first so I suppose it must be the "primary" entity or however it is called.
you shouldn't have to do either of those, that relationship should be discovered by convention
I do this so I could get the business entity from the address entity: Address.Business.Id for example
that's fine, i'm staying this is a basic relationship that you do not need to configure yourself
In this case it isn't very useful but I imagine that in the other entities I will need this and I guess it's better to have it the same everywhere
EF should automatically set it up
Are you talking about fluent api
yes
I get an error without it
Something along the lines of "can't determine parent"
Aka what points to what
weird
i'd personally make the address an owned entity
Maybe I'm just doing something wrong
Can you define owned entity
First time hearing it
it will be stored in-line in the business table
Oh so no mention in dbcontect as dbset?
so your business table gets columns like Address_Street, Address_City, etc
right
to me an address isn't an entity, it's just a value
so it doesn't make sense to treat it like an independent thing
Isn't this applicable to every one to one relationship
theoretically
but think of it this way
a business has an address, but does it necessarily make sense for an address to have a business or no business at all?
(it might, depends on your requirements)
I think an address should have a business because you could search up the street name or the city
And the business/es would pop up as results
you can do that whether it's owned or not, it just changes the table the data lives in
Oh or maybe you mean that
We never get the address as a separate entity, it is always interlocked with business
So it makes it useless to be seperate when you always call the business entity
right
Gotcha
I agree
so it sounds like there's no reason for an address to be able to exist in the database all by itself
Right
Okay I will work on that, thank you
I will close this thread now, good night 👋
iirc EFC 8 has better support for complex types without making them owned, could look into that too
I'm limited to net 6
ah, owned it is then