Here is what I want to do:
var user = db.User.First(conditions); user.Book.First();
This is how I should do it.
var user = db.User.Include("Book").First(conditionsForUser); user.Book.First();
The reason I want to simplify this is because I don’t want to indicate what is included every time I want to access the relationship. Seems very bulky.
For example: I would just like to do the following, given that I previously got the user:
user.Book.First() user.Blog.First() user.SomeOtherHasManyRelationship.Where(conditions)
Here is what I still have:
public object RelationshipFor(string relationship) { using (var db = User.DbContext()) { var relationshipType = TypeRepresentedBy(relationship);
And then, as it will be used, there will be the following:
user.RelationshipFor("Book")
I have other logic in my code that abstracts away what user.Book.First() would allow me to do. Hopefully I can get permission to open source because of this, as I am modeling a lot of api after crud in ActiveRecord style.
Please note that I am using a set of extensions that I have done to help manage dynamics less painful: https://github.com/NullVoxPopuli/csharp-extensions
UPDATE 1:
public object RelationshipFor(string relationship) { using (var db = User.DbContext()) { var myTable = (DbSet<DatabaseModels.User>)db.Send(RelationshipName); var myInclude = myTable.Include(i => i.Send(relationship)); var meWithRelationship = myInclude.First(i => (long)i.Send(IdColumn) == Id); return meWithRelationship.Send(relationship); } }
At the moment, I hard-coded the cast of the user, trying to just get something to work. My mistake:
Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN' to type 'System.Linq.Expressions.MemberExpression'.