【发布时间】:2016-07-29 13:07:02
【问题描述】:
我目前正在学习 Asp.NET MVC,我从 Vs2015 开始使用 Ouf of the Box 模板,但在从自定义表中获取数据时遇到问题
目标:我的目标是使用以下类为标准 ApplicationUser 添加联系人列表:
public class UserContact
{
[Key]
public virtual int Id { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual ApplicationUser Contact { get; set; }
}
我还向我的应用程序用户添加了以下内容:
public virtual ICollection<UserContact> ContactsList { get; set; }
下面一行到我的 ApplicationDbContext:
public DbSet<UserContact> UserContacts { get; set; }
问题是当我尝试从我的客户控制器 ContactController 访问 USerContacts 表时使用
ApplicationDbContext db = new ApplicationDbContext();
var UserId = SomUserID
db.UserContacts.Where(x => x.User == UserId)
where 子句根本无法识别。我已经使用了命令 启用迁移
添加-迁移联系人列表
更新数据库
运行没有错误,表现在在数据库中。但我无法访问该表。为什么?我究竟做错了什么?
请注意,根据 VS,就我而言,好像 .Where 函数在 DbSet 上不可用
并导入 linq,因为这是使用 Vs2015 添加新的空控制器时的标准。
数据库是在更新数据库期间创建的
但是 ApplicationDbContext : IdentiyDbContext 类不允许我访问任何其他表,包括 Contact 类,所以, 当我在我的控制器中使用时
"db.UserContacts.Where x.User == someID >();"
它无法识别来自 UserContact 类的 x.User 是什么。
【问题讨论】:
标签: asp.net asp.net-mvc visual-studio asp.net-identity