My data table is a list of articles written by authenticated users.
On the index page I would like to show a list of articles as well as the DisplayName of the author for each. The problem is the DisplayName is stored in my applicationDBContext identity table and my Data is stored in a DBContext (seperate databases on my server). The unique USERID key is on both tables, iD on the Identity table and Author ID on the Data table. I have tried different tihngs, giving different errors, the most common one being
"The specified LINQ expression contains references to queries that are associated with different contexts"
The code that generates that error is as follows:
var arts = (from a in db.Articles
join uid in iDdb.Users
on a.AuthorID equals uid.Id
select new ArticleVM
{
ID = a.ID,
ArticleTitle = a.ArticleTitle,
ArticleBody = a.ArticleBody.Substring(0, 200),
ArticleCategory = a.ArticleCategory,
DateCreated = a.DateCreated,
AuthorID = uid.DisplayName
}).AsEnumerable();
db.Articles in the Data tableon the data database and IDdb.Users is the User table (on the identity database). I can write an SQL query to generate what I need, but how do I put that data into a ViewModel, example, this SQL query will give me what I need:
var query = @"select artDB.*, usrDB.DisplayName from Article as artDB inner
join [IGTSIAppUsersTest1].dbo.AspNetUsers as usrDB on
artDB.AuthorID = usrDB.Id order by artDB.ID Desc";
var arts = db.Database.ExecuteSqlCommand(query);
Can anyone suggest a way to do this that is still somewhat efficient? I found one way to make it work but it requires a ridiculous amount of code and really hurts performance.