简体   繁体   English

连接身份表和数据表MVC5 EF6

[英]Join Identity Table and Data Table MVC5 EF6

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. 在索引页面上,我想显示文章列表以及每个文章的作者的DisplayName。 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). 问题是DisplayName存储在我的applicationDBContext标识表中,而我的数据存储在DBContext(服务器上的独立数据库)中。 The unique USERID key is on both tables, iD on the Identity table and Author ID on the Data table. 唯一的USERID密钥在两个表上,身份表上的iD和数据表上的作者ID。 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" “指定的LINQ表达式包含对与不同上下文关联的查询的引用”

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). 数据数据库上数据表中的db.Articles,而IDdb.Users是身份数据库上的User表。 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: 我可以编写一个SQL查询来生成所需的内容,但是如何将这些数据放入ViewModel中,例如,此SQL查询将为我提供所需的内容:

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. 我找到了一种使其工作的方法,但它需要大量的代码,并且确实会损害性能。

I found a way to do this. 我找到了一种方法。 It works as long as you are only viewing data and not changing data. 只要您仅查看数据而不更改数据,它就起作用。 I found the answer here 我在这里找到答案

    string query = (@"select a.*, usr.DisplayName as 'DisplayName' from article a
join identitytablename.dbo.AspNetUsers usr on
datatable.Id = a.authorid");
            IEnumerable<ArticleIndexVM> results = db.Database.SqlQuery<IndexViewModel>(query);

Works well as long as your ViemModel Types and names match up with the results of the query table names and types 只要您的ViemModel类型和名称与查询表名称和类型的结果匹配,就可以很好地工作

One of the approach here, you can use the same context for Users and Articles. 这里的一种方法是,您可以对用户和文章使用相同的上下文。 As following link below. 如下面的链接。

Use the same database for both my custom entities and identity context? 我的自定义实体和身份上下文都使用同一个数据库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM