I've been searching for an answer for hours. I've read many similar posts but still cannot get to the point. In geolocating an ASPNET MVC website I need to switch between 'n' tables depending on user ip. All tables have been named 'GeoData_' plus a convenient countryCode suffix (IT, FR, US...). Moreover, as they all contain the same fields, they inherit from the same 'GeoData' interface.
I know I can get needed dbset in the controller this way:
string countryTable = "GeoData_" + Server.HtmlEncode(Request.Cookies["_country"].Value.ToUpper());
var type = Assembly.GetExecutingAssembly()
.GetTypes()
.FirstOrDefault(t => t.Name == countryTable);
DbSet GeoContext = db.Set(type);
Now, I simply cannot pass this DbSet as I was used before, that is
var searchResult = GeoContext
.Where(c => c.PlaceName.ToUpper().Contains(term.ToUpper()))
.Select(c => new
{
PlaceName = c.PlaceName,
PostalCode = c.PostalCode,
GeoDataID = c.ID,
Latitude = c.Latitude,
Longitude = c.Longitude
}
)
.Distinct()
.OrderBy(c => (c.PlaceName.ToUpper().IndexOf(term.ToUpper())))
.ToList();
because model's fields (c.Placename etc...) can no more be referenced to GeoContext.
I suppose one way could be using System.Linq.Dynamic and changing the entire query consequently. Is there a way to take advantage of all tables inheriting from same interface to get a better workaround?
try defining the DbSet class explicitly like
DbSet<Class> GeoContext = db.Set<Class>(type);
i`m guessing you have this Class defined somewhere in your code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.