❔ Attaching record from one of multiple tables to another's entity property.
Heya, this time i'm struggling with Entity Framework. So I have main table
PaymentDatas
that holds mostly irrelevant data beside numeric value of which provider the rest of the data is stored. My goal is to have some sort of computed/virtual column, that when i query PaymentData
, in property provider
it has rest of the data pulled from specific provider. Thanks in advance!15 Replies
This sounds like it's possible. Could you share a bit of the code you attempted?
We can help you fix up code more easy than that we can write example code
Been mostly scraping internet looking for an answear, the closest i got to writing anything was:
For context it's inside of my DbContext class
Also, right now it yells because compiler found two similiar stuff like
System.Data.Entity.DbContext
and Microsoft.EntityFrameworkCore.DbContext
For better view ^is provider another entity in your system?
Yes
usually you would have
where you can do
https://learn.microsoft.com/en-us/ef/core/modeling/relationships this is a good read
Introduction to relationships - EF Core
How to configure relationships between entity types when using Entity Framework Core
The thing is that I'll have multiple providers and they will hold different data, so let's say in the
public Provider CurrentProvider { get; set; }
I'll hold either of PayPal, Stripe, PayU data
Thus why I'm in need of some sort of virtual/computed columnthis shouldn't change anything, as long as whatever it holds is a valid provider entity, the suggested solution should still work
So basically i should create parent class
Provider
and inherit from it for specific providers?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.So after some time I'm coming back to this issue, i put down the idea for the time when i implement at least one of the providers. So, what i came up with is to somehow make right join call on provider tables. This per se is not hard, but I'd like to somehow attach the data to one property on the main entity, preferably i'd like it to be eager loaded. When i was still pursuing this idea i thought about having some parent class, let's say
Provider
and inherit it for each of the provider types. Then after that in main entity i would just have the parent class as the type for the field, but when i done that it yelled with error Attempt to acess missing function
For context the code i still somewhat remember:
Same error was given when i changed the Provider
field to type object or dynamic. Is such stuff even possible? I'm coming from Node.js enviroment so i might even have the wrong idea of how to do it.
Thanks in advance again!Data classes really shouldn't do DB queries themselves, it's way better to have a class which properties are filled by the service
and maybe you need two
So I should just scrap the idea of having some sort of funky magic to connect these two tables, and just merge their info? Based on the relationship of course
Yeah
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.