C
C#2y ago
Connor

❔ How to setup the following relationship?

C#
public class Partner
{
public long Id { get; set; }
public ICollection<User> Users { get; set; }
}
public class User
{

public long Id { get; set; }
public long PartnerId { get; set; }
public Partner Partner { get; set; }
public ICollection<Ticket> Tickets { get; set; }
public class Ticket : ITrackedResource
{
public long Id { get; set; }
public long UserId { get; set; }
public User User { get; set; }
// public long PartnerId => User?.PartnerId; // would work with eager loading, lazy loading, or explicit loading but I don’t like any of those
}
public interface ITrackedResource
{
public long PartnerId { get; }
}
C#
public class Partner
{
public long Id { get; set; }
public ICollection<User> Users { get; set; }
}
public class User
{

public long Id { get; set; }
public long PartnerId { get; set; }
public Partner Partner { get; set; }
public ICollection<Ticket> Tickets { get; set; }
public class Ticket : ITrackedResource
{
public long Id { get; set; }
public long UserId { get; set; }
public User User { get; set; }
// public long PartnerId => User?.PartnerId; // would work with eager loading, lazy loading, or explicit loading but I don’t like any of those
}
public interface ITrackedResource
{
public long PartnerId { get; }
}
8 Replies
Connor
ConnorOP2y ago
There are certain resources in my db that belong to our partners, for those resources, I always need to know the partner Id it belongs to. What is the best way to set up the PartnerId property on those model. I don’t want to have to eager, lazy load, or explicitly load it. I just want it to always be populated. Some tracked resources belong directly to partners like Users in the example, some belong to partners through relationships, the Tickets in the example. The ones that only belong to partners through relationships, like tickets, is what’s causing me issues I pretty much what a way to do this via an attribute, convention, or configuring the db context.
ALTER TABLE dbo.Tickets
ADD PartnerId AS (SELECT PartnerId FROM dbo.Users WHERE Id = UserId);
ALTER TABLE dbo.Tickets
ADD PartnerId AS (SELECT PartnerId FROM dbo.Users WHERE Id = UserId);
Angius
Angius2y ago
Just... do Foreign keys are stored in the database Always They do not need to be loaded, lazily or eagerly
Connor
ConnorOP2y ago
So what should my Ticket model look like?
Angius
Angius2y ago
Ah, wait, it goes through User
Connor
ConnorOP2y ago
Is this is situation where I should use MultiTenanting? Not very familiar with it
Angius
Angius2y ago
Well, since you should be .Select()ing your results into DTOs anyway, what's the harm in loading it then?
Connor
ConnorOP2y ago
I need to be able to handle them generically when a property is changed On any entity that implements ITrackedResource Using reflection LazyLoading proxies with the commented out code is my best solution rn This isn’t exactly what I do but imagine the following code.
ITrackedResource trackedResource = entity;
var trackedResourceSubscriptions = await _context. TrackedResourceSubscriptions
.Where(x => x.PartnerId == trackedResource.PartnerId && … other conditions)
ITrackedResource trackedResource = entity;
var trackedResourceSubscriptions = await _context. TrackedResourceSubscriptions
.Where(x => x.PartnerId == trackedResource.PartnerId && … other conditions)
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server