'resolving foreign key asymmetries in a data model to reduce reliance on FromSqlRaw
simple example here:
public partial class human_being {
public int id {get; set;}
string first {get; set;}
string last {get; set;}
}
public class alienhuman {
...
public int alienID {get; set;}
[ForeignKey("activity")]
public int alienActivityID {get; set;}
public activity activity {get; set:}
...
}
and
public partial class activity {
/* a bunch of things */
public int ID {get; set;}
[ForeignKey("human_being")]
public int[] thx2048_humans {get; set}
/* this is where i am in a tizzy! this line below is commented out due to the issue described below */
public ICollection<human_being> whataTWISTEDobjectname {get; set;}
...
[ForeignKey("alienhumans")]
public ICollection<alienhuman> alienhumans;
}
now, if i pull the data for activity, i have no problem getting the integer array with the ids of each human_being.
similarly, i have no problem getting the collection of alienhuman objects since, i assume, there is a symmetric relationship between the keys:
//assume holderObject.ActivityCollection has successfully been assigned a collection of activity objects.
//these two lines work fine.
var localActivityObject = holderObject.ActivityCollection.SingleOrDefault(<some condition>);
await _context.Entry(localActivityObject).Collection(x => x.alienhumans).LoadAsync();
however, i would like to use this API to get the human_being collection as well, but:
await _context.Entry(localActivityObject).Collection(x => x.whataTWISTEDobjectname).LoadAsync();
perhaps "expectedly" fails, since this call tries to look for the column associated with activity.ID in human_being, which does not exist.
am i simply resigned to using FromSqlRaw unless i want to introduce unnecessary relationships?
keep in mind i'm new, so forgive me if i'm overlooking something.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
