linq2db / linq2db.EntityFrameworkCore

Bring power of Linq To DB to Entity Framework Core projects
MIT License
462 stars 38 forks source link

Is there a way to set the table name for a foreign table dynamically? #142

Closed gaspropot closed 3 years ago

gaspropot commented 3 years ago

For example:

var results = _dbContext.MyDbSet
    .ToLinqToDBTable().TableName($"my_{convention}")
    .LoadWithAsTable(p => p.GetForeignEntity).TableName($"my_ft_{convention}")
    .ToList()
sdanyliv commented 3 years ago

For which purpose? Maybe better to prepare several mapping schemas? Currently there is no possibility to remap related entities on the fly.

gaspropot commented 3 years ago

Please let me explain the problem. What I am trying to do is select from a dynamic table, then include another dynamic table to my result which is related to the first table with a one-to-one relationship. So far I have managed to select from my first dynamic table by using the method .TableName($"{dynamic_table_name}"), and now for the next step I need to do something similar so that I include the second dynamic table that is related to the first one, like I try to do with the above linq query. Is there a way to do that?

sdanyliv commented 3 years ago

I would suggest only custom projection:

var query = 
    from p in _dbContext.MyDbSet.ToLinqToDBTable().TableName($"my_{convention}")
    from r in _dbContext.RelatedDbSet.ToLinqToDBTable().TableName($"my_ft_{convention}").LeftJoin(r => r.Parentid == p.Id)
    select new MyDbSet
    {
        Id = p.Id,
        ...
        GetForeignEntity = r
    };

var results = query.ToList();
gaspropot commented 3 years ago

I have decided to go with a different approach in order to reduce complexity. Thank you for trying to help anyways.