Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC使用SimpleMembership检索用户名_Asp.net Mvc_Entity Framework_Ef Code First_Simplemembership_Ef Database First - Fatal编程技术网

Asp.net mvc MVC使用SimpleMembership检索用户名

Asp.net mvc MVC使用SimpleMembership检索用户名,asp.net-mvc,entity-framework,ef-code-first,simplemembership,ef-database-first,Asp.net Mvc,Entity Framework,Ef Code First,Simplemembership,Ef Database First,我正在创建一个论坛,最近我实现了简单的成员资格。我现在要做的是显示写这篇文章的人的真实姓名。到目前为止,我只能显示用户的UserId 我有两个模型:Simplemembership模型AccountModels和我的ForumModels。 My Forummodel Posts包含一个UserId字段 我尝试将AccountModels中的一些表添加到我的ForumModels中,但这只是导致了一个错误(因为我尝试两次创建同一个表) 我尝试创建一个包含POST和UserProfile的View

我正在创建一个论坛,最近我实现了简单的成员资格。我现在要做的是显示写这篇文章的人的真实姓名。到目前为止,我只能显示用户的UserId

我有两个模型:Simplemembership模型AccountModels和我的ForumModels。 My Forummodel Posts包含一个UserId字段

我尝试将AccountModels中的一些表添加到我的ForumModels中,但这只是导致了一个错误(因为我尝试两次创建同一个表)

我尝试创建一个包含POST和UserProfile的ViewModel,但无法用数据正确填充它

最后,我尝试在两个表Post和Userprofile上执行联接

   var posts = (from p in db.Posts
                     join u in udb.UserProfiles
                     on p.UserId equals u.UserId
                     where p.TopicId == id
                     select p);
        return View(posts);
这导致了以下错误:

指定的LINQ表达式包含对与不同上下文关联的查询的引用


关于我应该做什么有什么想法吗?

看来您正在尝试在两个不同的上下文之间执行连接。您可以尝试:

1) 调用第一个上下文并将ID集合保存在如下列表中:

var userIds = udb.UserProfiles.UserId.ToList();
var posts = from p in db.Posts 
    where p.TopicId == id && userIds.Contains(p.UserId) 
    select p;
2) 将帖子添加到与简单成员使用的内容相同的上下文中,您将能够使用join

更新示例代码

//This will retrieve the posts from the database. 
//ToList() will translate and execute the SQL statement, 
//so you can manipulate the colleciton in memory
var posts = (from p in db.Posts 
    where p.TopicId == id
    select p).ToList();

//Get all the user IDs on the post collection. 
var userIds = posts.Select(p => p.UserId).Distinct();

//Then we will select the users in the posts
var users = ubd.UserProfiles.Where(u => userIds.Contains(u.UserId)).ToList();

//Now you have both collections in memory and you can perform the join
var joined = from p in posts
             join u in users
             on p.UserId equals u.UserId
             select new {Title = p.Title, UserName = u.UserName};

谢谢你的回复。在1中的第一行,我得到的
没有包含UserId的定义
我的udb是
私有用户上下文udb=newuserscontext()2)我是MVC新手,不完全了解什么是上下文以及如何向上下文添加“帖子”。你的问题不完全是MVC的问题,而是实体框架的问题。您使用的是SimpleMembership,它使用“代码优先”方法,将实体定义为类,并将它们转换为SQL表。这里的问题是表是在两个不同的上下文中定义的(一个用于简单成员身份,另一个用于数据存储)。我会用一些可以让你得到名字的东西来更新答案。我已经试过你的新代码了。我不得不添加属性“p.Title”,但是我从视图中得到了这个错误
传递到字典中的模型项的类型为“System.Linq.Enumerable+d_u61
4[Forum3.Models.Posts,Forum3.Models.UserProfile,System.Int32,f_uAnonymousType4
2[System.String,System.String]],但此字典需要类型为“System.Collections.Generic.IEnumerable
1[Forum3.Models.Posts]“不知道我应该在视图中使用什么作为模型,我添加了“title”作为示例。我不知道你们桌子的结构。我生成的是一个匿名对象。为了将对象绑定到视图,必须创建一个新类,即ViewModel,它将具有需要在屏幕上显示的组合属性。您的视图正在抛出错误,因为它期望IEnumerable作为模型,但您正在传递匿名对象…是否先使用代码并添加迁移来在ForumModels中创建数据库?My ForumModels是databasefirst。简单的成员资格是codefirst。